Qt: integer formatting in QString

I would like to format the integer in QString. I would always like to have 6 rooms. For example, “1” should be “000001” and “12” should be “000012”.

I am trying to do with help printf(%06d, number). Therefore i wrote this

QString test; test = QString("%06d").arg(QString::number(i)); qDebug()<<test;

i is implemented in a loop for. But this did not work, since I have:

"0d" "1d" "2d" "3d" ...

Does anyone know how to do this, please?

+4
source share
3 answers

String argument support does not work like printf. All of this is documented. Do you want to:

QString test = QString("%1").arg(i, 6, 10, QLatin1Char('0'));
+4

:

int a = 12;
QString test = QString("%1").arg(a, 6, 'g', -1, '0');
qDebug() << test; // outputs "000012"
0

See the documentation for QTextStream . There are many formatting options, as well as several convenient manipulators. This is similar to text manipulators from STLiostream

0
source

Source: https://habr.com/ru/post/1528766/


All Articles