QString:: arg()方法

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 04:48   11   0



转载自:点击打开链接http://blog.163.com/qimo601@126/blog/static/1582209320134993927300/



QString 有多种方法,我慢慢总结中~~~~~~~~~~~~

1、QString::arg()//用字符串变量参数依次替代字符串中最小数值

Cpp代码
  1. QString i = "iTest"; // current file's number
  2. QString total = "totalTest"; // number of files to process
  3. QString fileName = "fileNameTest"; // current file's name
  4. QString status = QString("Processing file %1 of %2: %3")
  5. .arg(i).arg(total).arg(fileName);
  6. style="background-color: #ffffff;"> qDebug() << status ;

结果就是:"Processing file iTest of totalTest: fileNameTest"

First, arg(i) replaces %1. Then arg(total) replaces %2. Finally, arg(fileName) replaces %3.

fieldWidth specifies the minimum amount of space that argument a shall occupy. If a requires less space than fieldWidth, it is padded to fieldWidthwith character fillChar. A positive fieldWidth produces right-aligned text. A negative fieldWidth produces left-aligned text.


2、QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const

a表示输入的值,fieldWidth表示字符宽度,base表示进制,fillChar 表示填充字符

若是字符宽度fieldWidth是正数,fillchar填在左边,若是负数,fillchar填在右边

16进制输出:

Cpp代码
  1. QString str;
  2. str = QString("Decimal 63 is %1 in hexadecimal")
  3. .arg(63, 0, 16);
  4. // str == "Decimal 63 is 3f in hexadecimal"
  5. QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
  6. str = QString("%1 %L2 %L3")
  7. .arg(12345)
  8. .arg(12345)
  9. .arg(12345, 0, 16);
  10. // str == "12345 12,345 3039"
  11. //16进制显示,就忽略%L3的L
  12. //格式化输出
  13. str = QString("%1").arg(521,5,10,QChar('a'))
  14. //str =="aa521"

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP