QString:: arg()方法



                  轉載自:點擊打開鏈接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.   
  5. QString status = QString("Processing file %1 of %2: %3")  
  6.                 .arg(i).arg(total).arg(fileName);  
  7.  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.   
  6. QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));  
  7. str = QString("%1 %L2 %L3")  
  8.         .arg(12345)  
  9.         .arg(12345)  
  10.         .arg(12345, 0, 16);  
  11. // str == "12345 12,345 3039"  
  12. //16進制顯示,就忽略%L3的L  
  13. //格式化輸出
  14. str = QString("%1").arg(521,5,10,QChar('a'))
  15. //str =="aa521"

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章