Concatenating two QStrings with an integer

Karim M. El Tel picture Karim M. El Tel · Aug 10, 2011 · Viewed 27.8k times · Source

I want to do something like this in C++ using Qt:

int i = 5;
QString directory = ":/karim/pic" + i + ".jpg";

where + means I want to concatenate the strings and the integer (that is, directory should be :/karim/pic5.jpg). How can I do this?

Answer

Sebastian Negraszus picture Sebastian Negraszus · Aug 10, 2011

Qt's idiom for things like this is the arg()function of QString.

QString directory = QString(":/karim/pic%1.jpg").arg(i);