C++ 如何将 int 转换为 QString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3211771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:19:53  来源:igfitidea点击:

How to convert int to QString?

c++qttype-conversion

提问by Ahmad

Is there a QStringfunction which takes an intand outputs it as a QString?

是否有一个QString函数接受一个int并将其输出为 a QString

回答by Georg Fritzsche

Use QString::number():

使用QString::number()

int i = 42;
QString s = QString::number(i);

回答by Kamil Klimek

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);

回答by Gabriel de Grimouard

Moreover to convert whatever you want, you can use QVariant. For an intto a QStringyou get:

此外,要转换您想要的任何内容,您可以使用QVariant. 对于 aint到 aQString你得到:

QVariant(3).toString();

A floatto a stringor a stringto a float:

Afloat到 astring或 astring到 a float

QVariant(3.2).toString();
QVariant("5.2").toFloat();

回答by Matthew Kraus

Yet another option is to use QTextStreamand the <<operator in much the same way as you would use coutin C++:

另一种选择是以与在 C++ 中使用的方式大致相同的方式使用QTextStream<<运算符cout

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.

由于运算符<<()已重载,因此您可以将其用于多种类型,而不仅仅是int. QString::arg()例如arg(int a1, int a2),被重载,但没有arg(int a1, QString a2),因此在使用混合类型格式化较长的字符串时,使用QTextStream()and 运算符<<很方便。

Caution:You might be tempted to use the sprintf()facility to mimic C style printf()statements, but it is recommended to use QTextStreamor arg()because they support Unicode strings.

注意:您可能想使用该sprintf()工具来模拟 C 风格的printf()语句,但建议使用QTextStreamarg()因为它们支持 Unicode strings。

回答by Narek

I always use QString::setNum().

我总是使用QString::setNum().

int i = 10;
double d = 10.75;
QString str;
str.setNum(i);
str.setNum(d);

setNum()is overloaded in many ways. See QStringclass reference.

setNum()在很多方面都超载了。请参阅QString类参考。

回答by liaK

In it's simplest form, use the answer of Georg Fritzsche

以最简单的形式,使用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..

此处获取文档和示例..

回答by Morgan

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

为了完整起见,您可以使用标准库并执行 QString qstr = QString::fromStdString(std::to_string(42));

回答by André

If you need locale-aware number formatting, use QLocale::toString instead.

如果您需要识别区域设置的数字格式,请改用 QLocale::toString。