多个数值转QString
int, float, double等数值类型转换为QString的方法
1. 用QTextStream
QTextStream类可以用数据流的方式直接将任意多个数值、字符、字符串等传入QString。举例如下:
QString strData; double a1 = 3.14; int a2 798; QTextStream(&strData) <<a1 <<"-"<<a2 ;
用法还比较简单,但是设置数值类型的格式不太方便。
2. 用QString::arg() (推荐)
QString::arg()是QString类中的一个重载函数,可以转换各种数值类型,并设置输出格式。
下面是Qt官方文档中的说明。
QString arg(const QString & a, int fieldWidth = 0, QChar fillChar = QLatin1Char( ' ' )) const QString arg(const QString & a1, const QString & a2) const QString arg(const QString & a1, const QString & a2, const QString & a3) const QString arg(const QString & a1, const QString & a2, const QString & a3, const QString & a4) const QString arg(const QString & a1, const QString & a2, const QString & a3, const QString & a4, const QString & a5) const QString arg(const QString & a1, const QString & a2, const QString & a3, const QString & a4, const QString & a5, const QString & a6) const QString arg(const QString & a1, const QString & a2, const QString & a3, const QString & a4, const QString & a5, const QString & a6, const QString & a7) const QString arg(const QString & a1, const QString & a2, const QString & a3, const QString & a4, const QString & a5, const QString & a6, const QString & a7, const QString & a8) const QString arg(const QString & a1, const QString & a2, const QString & a3, const QString & a4, const QString & a5, const QString & a6, const QString & a7, const QString & a8, const QString & a9) const QString arg(int a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(uint a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(long a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(ulong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(qlonglong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(qulonglong a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(short a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(ushort a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const QString arg(QChar a, int fieldWidth = 0, QChar fillChar = QLatin1Char( ' ' )) const QString arg(char a, int fieldWidth = 0, QChar fillChar = QLatin1Char( ' ' )) const QString arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char( ' ' )) const
以int类型为例,fieldWidth表示整个数值所占的长度,base表示进制数(如10,8,2等),fillChar表示填充的字符。
int a1 = 123; int a2 = 38; QString strData = QString("%1-%2").arg(a1, 3, 10, QChar('0')).arg(a2, 3, 10, QChar('0'));
这样strData = "123-038"
关于QString("%1-%2"),可以参看官方说明:
QString status = QString("Processing file %1 of %2: %3").arg(i).arg(total).arg(fileName);
“First, arg(i) replaces %1. Then arg(total) replaces %2. Finally, arg(fileName) replaces %3.”
也就是说QString("%1-%2")中的"%1"和"%2"分别对应a1的arg()和a2的arg()。