QString str = "3.14";
bool ok;
double num = str.toDouble(&ok);
if (ok) {
// Conversion successful
// 'num' contains the double value of the string
} else {
// Conversion failed
// Handle the error here
}
- Declare a
QString
variable named str
and assign it the value "3.14"
. - Declare a boolean variable
ok
. - Use the
toDouble()
function of the QString
class to convert the string str
to a double
value. The &ok
parameter is a pointer to the ok
variable, which will be set to true
if the conversion is successful and false
otherwise. - Check the value of
ok
. If it is true
, the conversion was successful, and the variable num
now holds the double
value of the string. Otherwise, the conversion failed, and you can handle the error accordingly.