Google
 

12/14/09

QString and number format.

Question: how to construct 0001.tif, 0002.tif, ..... and 0010.tif using QString?

With C++, one can use sprintf to do this. sprintf(str, '%04d', i) where str is the output string, i is the iteration number (from 1 to 10). With QString, one can also use sprintf, but as the document mentions it is not the recommended way. Instead, using arg is better.

Here is one example showing how to use arg to construct number format.


#include <_QtCore/QCoreApplication>
#include <_QString>
#include <_QDebug>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    uint d = 90;
    QString test("9 is %1");
    const QChar fillChar = QLatin1Char('0');
    QString testNew = test.arg(d, 5, 10, fillChar);
    qDebug() << "string is: " << testNew;

    return a.exec();
}

the output is:

string is: "9 is 00090" 

No comments: