Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt: format an integer in a QString

I would like to format an integer in a QString. I would like to always have 6 numbers. For example "1" should be "000001" and "12" should be "000012".

I try to do like with printf(%06d, number). So I wrote this

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

i is implemented in a loop for. But it does not work since I have:

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

Does anyone know how to do this please?

like image 683
Jeanstackamort Avatar asked Feb 25 '14 14:02

Jeanstackamort


People also ask

How do you split a QString?

To break up a string into a string list, we used the QString::split() function. The argument to split can be a single character, a string, or a QRegExp. To concatenate all the strings in a string list into a single string (with an optional separator), we used the join() function.

What is QT QString?

The QString class provides an abstraction of Unicode text and the classic C '\0'-terminated char array. More... All the functions in this class are reentrant when Qt is built with thread support.

How do you initialize QString?

Generally speaking: If you want to initialize a QString from a char*, use QStringLiteral . If you want to pass it to a method, check if that method has an overload for QLatin1String - if yes you can use that one, otherwise fall back to QStringLiteral .


1 Answers

String's argument support doesn't work like printf. It's all documented. What you want is:

QString test = QString("%1").arg(i, 6, 10, QLatin1Char('0'));
like image 192
Kuba hasn't forgotten Monica Avatar answered Oct 04 '22 04:10

Kuba hasn't forgotten Monica