int, float, double等數值類型轉換爲QString的方法函數
1. 用QTextStreamui
QTextStream類能夠用數據流的方式直接將任意多個數值、字符、字符串等傳入QString。舉例以下:spa
QString strData; double a1 = 3.14; int a2 798; QTextStream(&strData) <<a1 <<"-"<<a2 ;
用法還比較簡單,可是設置數值類型的格式不太方便。code
2. 用QString::arg() (推薦)orm
QString::arg()是QString類中的一個重載函數,能夠轉換各類數值類型,並設置輸出格式。blog
下面是Qt官方文檔中的說明。ci
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()。