简体   繁体   English

将bool转换为QString

[英]Convert bool to QString

I want convert bool to QString. 我想将bool转换为QString。

Whats the most efficient way to do it?, This is my code but sure that there is other way better. 什么是最有效的方法?这是我的代码,但确定还有其他更好的方法。

bool test = true;
test ? "1" : "0";

Thanks. 谢谢。

You can use the static QString::number method - the bool will be implicitly cast to int to match the integer form of the static factory method, which returns a QString containing 0 or 1 . 您可以使用静态QString::number方法 - 将bool隐式转换为int以匹配静态工厂方法的整数形式,该方法返回包含01QString

bool test = true;
QString s = QString::number(test);

qDebug() displays a bool variable as "true" or "false". qDebug()将bool变量显示为“true”或“false”。 If you want to get such a string you can change your code a little bit: 如果你想得到这样一个字符串,你可以稍微改变你的代码:

bool test = true;
QString boolText = test ? "true" : "false";

Use QVariant! 使用QVariant!

From bool to QString: 从bool到QString:

   bool bInput = false;
   QString s = QVariant(bInput).toString();

From QString to bool: 从QString到bool:

  QString s = "true";
  bool bInUse = QVariant(s).toBool();    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM