简体   繁体   中英

Convert bool to QString

I want convert bool to 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 .

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

qDebug() displays a bool variable as "true" or "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!

From bool to QString:

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

From QString to bool:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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