Is there a QString
function which takes an int and outputs it as a QString
?
9 Answers
Use QString::number()
:
int i = 42;
QString s = QString::number(i);

- 5,577
- 2
- 27
- 38

- 97,545
- 26
- 194
- 236
And if you want to put it into string within some text context, forget about +
operator.
Simply do:
// Qt 5 + C++11
auto i = 13;
auto printable = QStringLiteral("My magic number is %1. That's all!").arg(i);
// Qt 5
int i = 13;
QString printable = QStringLiteral("My magic number is %1. That's all!").arg(i);
// Qt 4
int i = 13;
QString printable = QString::fromLatin1("My magic number is %1. That's all!").arg(i);

- 95,931
- 16
- 151
- 313

- 12,884
- 2
- 43
- 58
-
3Since you mention the `+` operator, careful around integers, since it might very well work but internally, the `operator+(const QString &s, char c)` implementation is called, and the string wont contain the integer as number but its `QChar::fromAscii(c)` equivalent – x29a Jul 23 '15 at 08:19
-
4Since you mention the + operator, you can actually do it, but understanding whats happening: QString p = s + QString::number(1); being s a QString works perfectly. So, basically QString + QString is okay, QString + int **bad**. – David Sánchez Aug 12 '15 at 09:25
Moreover to convert whatever you want, you can use QVariant
.
For an int
to a QString
you get:
QVariant(3).toString();
A float
to a string
or a string
to a float
:
QVariant(3.2).toString();
QVariant("5.2").toFloat();

- 1,917
- 15
- 22
Yet another option is to use QTextStream
and the <<
operator in much the same way as you would use cout
in C++:
QPoint point(5,1);
QString str;
QTextStream(&str) << "Mouse click: (" << point.x() << ", " << point.y() << ").";
// OUTPUT:
// Mouse click: (5, 1).
Because operator <<()
has been overloaded, you can use it for multiple types, not just int
. QString::arg()
is overloaded, for example arg(int a1, int a2)
, but there is no arg(int a1, QString a2)
, so using QTextStream()
and operator <<
is convenient when formatting longer strings with mixed types.
Caution: You might be tempted to use the sprintf()
facility to mimic C style printf()
statements, but it is recommended to use QTextStream
or arg()
because they support Unicode string
s.

- 5,031
- 17
- 33
- 41

- 6,660
- 5
- 24
- 31
I always use QString::setNum()
.
int i = 10;
double d = 10.75;
QString str;
str.setNum(i);
str.setNum(d);
setNum()
is overloaded in many ways. See QString
class reference.

- 4,167
- 3
- 25
- 30

- 38,779
- 79
- 233
- 389
A more advanced way other than the answer of Georg Fritzsche:
QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const
Get the documentation and an example here.

- 5,031
- 17
- 33
- 41

- 11,422
- 11
- 48
- 73
If you need locale-aware number formatting, use QLocale::toString
instead.

- 5,031
- 17
- 33
- 41

- 570
- 3
- 7
Just for completeness, you can use the standard library and do:
QString qstr = QString::fromStdString(std::to_string(42));

- 5,031
- 17
- 33
- 41

- 346
- 1
- 7
QLocale has a handy way of converting numbers. It's not much more typing than the accepted answer, but is more useful in the case of floats; so I like to do both this way. Here's for an int:
int i = 42;
QString s = QLocale().toString(i);
and here's for a float:
float f=42.5;
QString s = QLocale().toString(f, 1);
the last argument is the number of decimal places. You can also insert a char format argument such as 'f' or 'e' for the second parameter. The advantage of this, is if your program is then run in a locale where a comma is used as a decimal "point", it will automatically print it that way. It's not included in something like <QCoreApplication>
, so you'll have to do an #include <QLocale>
somewhere, of course. It really comes into its own in formatting currency strings.
It has the slight performance downside of requiring the creation and deletion of an object during the evaluation, but were performance an issue, you could just allocate it once, and use it repeatedly.
You could write:
QString s = QString::number(42.5, 'f', 1);
but according to the help "Unlike QLocale::toString(), this function does not honor the user's locale settings."

- 1,248
- 13
- 22