Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending number to QString with arg() , is there better ways?

Tags:

qt

qstring

qt4.7

I've been using QString::number () to convert numbers to string for long time , now i'm wondering if there's something better than following:

  int i = 0;
  QString msg = QString ("Loading %1").arg (QString::number (i));

How can i spare QString::number () ? i checked document , seems only "%1" is applicable , no other stuff like "%d" could work

like image 777
daisy Avatar asked Nov 24 '11 10:11

daisy


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.

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 .

What is QString C++?

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.


1 Answers

QString's arg() function does indeed implement many different types and automatically detect what you provide it. Providing multiple parameters to a single arg() call like so

// Outputs "one two three"
QString s = QString("%1 %2 %3").arg("one", "two", "three")

is only implemented for QString (and hence const char*) parameters.

However, you can chain arg calls together and still use the numbering system:

int i = 5;
size_t ui = 6;
int j = 12;
// Outputs "int 5 size_t 6 int 12"
qDebug() << QString("int %1 size_t  %2 int%3").arg(i).arg(ui).arg(j);
// Also outputs "int 5 size_t 6 int 12"
qDebug() << QString("int %1 int %3 size_t %2").arg(i).arg(j).arg(ui);
like image 145
Tim MB Avatar answered Oct 04 '22 15:10

Tim MB