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.

Answers


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();    

Need Your Help

How to start redis-server on a different port than the default port 6379 in ubuntu

redis port ubuntu-14.04 redis-server

How to start redis-server on a different port than the default port 6379 in ubuntu

Attribute routing and inheritance

asp.net-web-api asp.net-web-api-routing

I am playing around with the idea of having a base controller that uses a generic repository to provide the basic CRUD methods for my API controllers so that I don't have to duplicate the same basi...