Existuje funkcia QString
, ktorá prevezme int a vypíše ho ako QString
?
A ak ho chcete vložiť do reťazca v rámci nejakého textového kontextu, zabudnite na operátor +
.
Jednoducho urobte:
// 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);
V najjednoduchšej forme použite odpoveď Georga Fritzscheho
Pre trochu pokročilých môžete použiť túto,
QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const
Získajte dokumentáciu a príklad tu.