Is there a QString function as an argument that takes an int type and returns it as a QString ?
Translation of the question β How to convert int to QString? Β» @Ahmad .
Is there a QString function as an argument that takes an int type and returns it as a QString ?
Translation of the question β How to convert int to QString? Β» @Ahmad .
Use QString::number() :
int i = 42; QString s = QString::number(i); You can use the arg method:
QString QString::arg ( int a, int fieldWidth = 0, int base = 10, const QChar & fillChar = QLatin1Char( ' ' ) ) const And if you want to use a solution when concatenating strings, forget about the + operator. Just do this:
// 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); Another way is to use QTextStream and the << operator in much the same way 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). Since the operator << overridden, it can be used for several types, not only for int . QString::arg() overridden, for example, arg(int a1, int a2) , but there is no arg(int a1, QString a2) , so using QTextStream() and the operator << convenient when formatting longer strings with mixed types.
Warning : you may want to use sprintf() to play C-style printf() statements, but I recommend using QTextStream or arg() , as they support Unicode strings.
Take a look at QString::setNum() .
int i = 10; double d = 10.75; QString str; str.setNum(i); str.setNum(d); setNum() redefined in many ways. See the QString Class Reference.
Moreover, you can use QVariant to convert from various formats to various formats. To convert int to QString we get:
QVariant(3).toString(); To convert float to String or String to float :
QVariant(3.2).toString(); QVariant("5.2").toFloat(); Based on the answers from the question β How to convert int to QString? ".
Source: https://ru.stackoverflow.com/questions/519913/
All Articles