[英]Convert bool to QString
我想將bool轉換為QString。
什么是最有效的方法?這是我的代碼,但確定還有其他更好的方法。
bool test = true;
test ? "1" : "0";
謝謝。
您可以使用靜態QString::number
方法 - 將bool隱式轉換為int以匹配靜態工廠方法的整數形式,該方法返回包含0
或1
的QString
。
bool test = true;
QString s = QString::number(test);
qDebug()將bool變量顯示為“true”或“false”。 如果你想得到這樣一個字符串,你可以稍微改變你的代碼:
bool test = true;
QString boolText = test ? "true" : "false";
使用QVariant!
從bool到QString:
bool bInput = false;
QString s = QVariant(bInput).toString();
從QString到bool:
QString s = "true";
bool bInUse = QVariant(s).toBool();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.