17
我想將bool轉換爲QString。將bool轉換爲QString
什麼是最有效的方法呢?,這是我的代碼,但肯定還有其他更好的方法。
bool test = true;
test ? "1" : "0";
謝謝。
我想將bool轉換爲QString。將bool轉換爲QString
什麼是最有效的方法呢?,這是我的代碼,但肯定還有其他更好的方法。
bool test = true;
test ? "1" : "0";
謝謝。
可以使用靜態QString::number
方法 - 的布爾將被隱式轉換爲int來匹配靜態工廠方法,它返回一個含有QString
或0
1
的整數形式。
bool test = true;
QString s = QString::number(test);
qDebug()將bool變量顯示爲「true」或「false」。如果你想得到這樣的字符串,你可以改變你的代碼一點點:
bool test = true;
QString boolText = test ? "true" : "false";
沒有更好的方式,我知道。 – Koying 2012-03-07 15:45:14
不應該是'QString(test?「1」:「0」)'? – 2012-03-07 15:49:06
是larsmans對不起,我的複製/粘貼不能很好地對不起 – Jjreina 2012-03-07 15:54:30