Is there a QString
function which takes an int and outputs it as a QString
?
8 Answers
Use QString::number()
:
int i = 42;
QString s = QString::number(i);
![](../../users/profiles/1702266.webp)
- 4,567
- 2
- 25
- 37
![](../../users/profiles/168225.webp)
- 93,086
- 26
- 183
- 232
-
2Here's the more interesting question: is there a faster way? I have encountered a problem where this version almost takes more time than the entire processing afterwards... – Zeks Apr 04 '17 at 18:22
-
1Then you probably want to work on pre-allocated buffers... but this sounds like a separate question. – Georg Fritzsche Apr 06 '17 at 04:45
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);
![](../../users/profiles/1329652.webp)
- 88,505
- 13
- 129
- 275
![](../../users/profiles/223880.webp)
- 12,274
- 2
- 37
- 55
-
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();
![](../../users/profiles/4538963.webp)
- 1,739
- 13
- 19
-
Call me nuts, but I'd get more use out of the question as "How to convert a number to QString?", this as the main answer for lightweight conversions, and the other answers for special treatments. – mr3 Jul 18 '17 at 23:47
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.
![](../../users/profiles/4904417.webp)
- 3,308
- 3
- 20
- 30
![](../../users/profiles/2452084.webp)
- 6,200
- 5
- 20
- 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.
![](../../users/profiles/4904417.webp)
- 3,308
- 3
- 20
- 30
![](../../users/profiles/163394.webp)
- 35,407
- 69
- 202
- 359
In it's simplest form, use the answer of Georg Fritzsche
For a bit advanced, you can use this,
QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const
Get the documentation and an example here..
Just for completeness, you can use the standard library and do QString qstr = QString::fromStdString(std::to_string(42));
![](../../users/profiles/1327591.webp)
- 336
- 1
- 7