O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Converting between numbers and strings

QString also provides some methods for convenient conversion between textual and numerical values. Methods such as toInt(), toDouble(), or toLongLong() make it easy to extract numerical values from strings. All such methods take an optional bool *ok parameter. If you pass a pointer to a bool variable as this parameter, the variable will be set to true or false, depending on whether the conversion was successful or not. Methods returning integers also take the second optional parameter that specifies the numerical base (for example, binary, octal, decimal, or hexadecimal) of the value:

bool ok; int v1 = QString("42").toInt(&ok, 10); // v1 = 42, ok = true long long v2 = QString("0xFFFFFF").toInt(&ok, 16); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required