Qt provides a set of classes and functions for easily converting between different types of data. This makes Qt more than just a GUI library; it is a complete platform for software development. The QVariant
class, which we will be using in the following example, makes Qt even more flexible and powerful compared to similar conversion functionalities provided by the C++ standard library.
Let's learn how to convert various data types in Qt by following these steps:
main.cpp
and add the following headers to it:#include <QCoreApplication> #include <QDebug> #include <QtMath> #include <QDateTime> #include <QTextCodec> #include <iostream>
main()
function, add the following code to convert a string to a number:int numberA = 2; QString numberB = "5"; qDebug() << "1) " << "2 + 5 =" << numberA + numberB.toInt();
float numberC = 10.25; float numberD = 2; QString result = QString::number(numberC * numberD); qDebug() << "2) " << "10.25 * 2 =" << result;
qFloor()
:float numberE = 10.3; float numberF = qFloor(numberE); qDebug() << "3) " << "Floor of 10.3 is" << numberF;
qCeil()
, we are able to round a number to the smallest integral value not smaller than its initial value:float numberG = 10.3; float numberH = qCeil(numberG); qDebug() << "4) " << "Ceil of 10.3 is" << numberH;
QString dateTimeAString = "2016-05-04 12:24:00"; QDateTime dateTimeA = QDateTime::fromString(dateTimeAString, "yyyy-MM-dd hh:mm:ss"); qDebug() << "5) " << dateTimeA;
QDateTime dateTimeB = QDateTime::currentDateTime(); QString dateTimeBString = dateTimeB.toString("dd/MM/yy hh:mm"); qDebug() << "6) " << dateTimeBString;
QString::toUpper()
function to convert a string variable to all capital letters:QString hello1 = "hello world!"; qDebug() << "7) " << hello1.toUpper();
QString::toLower()
will convert the string to all lowercase:QString hello2 = "HELLO WORLD!"; qDebug() << "8) " << hello2.toLower();
QVariant
class provided by Qt is a very powerful data type that can be easily converted to other types without any effort by the programmer:QVariant aNumber = QVariant(3.14159); double aResult = 12.5 * aNumber.toDouble(); qDebug() << "9) 12.5 * 3.14159 =" << aResult;
QVariant
variable can be simultaneously converted to multiple data types without any effort by the programmer:qDebug() << "10) "; QVariant myData = QVariant(10); qDebug() << myData; myData = myData.toFloat() / 2.135; qDebug() << myData; myData = true; qDebug() << myData; myData = QDateTime::currentDateTime(); qDebug() << myData; myData = "Good bye!"; qDebug() << myData;
All the data types provided by Qt, such as QString
, QDateTime
, QVariant
, and so on, contain functions that make conversion to other types easy and straightforward.
Qt also provides its own object conversion function, qobject_cast()
, which doesn't rely on the standard library. It is also more compatible with Qt and works well for converting between Qt's widget types and data types.
Qt also provides you with the QtMath
class, which helps you to manipulate number variables, such as rounding up a floating point number or converting an angle from degrees to radians.
QVariant
is a special class that can be used to store data of all kinds of type. It can automatically determine the data type by examining the value stored in the variable. You can also easily convert the data to any of the types supported by the QVariant
class by just calling a single function, such as toFloat()
, toInt()
, toBool()
, toChar()
, toString()
, and so on.
Be aware that each of these conversions takes computing power to make it happen. Even though modern computers are extremely fast at handling operations such as these, you should be careful not to overdo it with a large quantity at the same time. If you're converting a large set of variables for complex calculations, it might slow down your computer significantly, so therefore try to convert variables only whenever it's deemed necessary.
3.21.244.217