QVariant number = 3.14159; qreal realNumber = number.toReal(); qDebug() << "Real value of QVariant object is: " << realNumber;
QVariant string = "Hello, world!"; qreal realString = string.toReal(); qDebug() << "Real value of QVariant object is: " << realString;In this example, a QVariant object named string is created with a string value of "Hello, world!". However, since the toReal function can only extract real numbers, attempting to extract a string value results in a returned value of 0. The debug function is used to print out the value of realString, which is 0. Package library: Qt.