Data conversion

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.

How to do it…

Let's learn how to convert various data types in Qt by following these steps:

  1. Open up Qt Creator and create a new Qt Console Application project by going to File | New File or Project:
    How to do it…
  2. Next, open up main.cpp and add the following headers to it:
    #include <QCoreApplication>
    #include <QDebug>
    #include <QtMath>
    #include <QDateTime>
    #include <QTextCodec>
    #include <iostream>
  3. Then, in the 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();
  4. After that, we'll convert a number back to a string:
    float numberC = 10.25;
    float numberD = 2;
    QString result = QString::number(numberC * numberD);
    qDebug() << "2) " <<  "10.25 * 2 =" << result;
  5. We also learn how to round down a value by using qFloor():
    float numberE = 10.3;
    float numberF = qFloor(numberE);
    qDebug() << "3) " << "Floor of 10.3 is" << numberF;
  6. Then, by using 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;
  7. After that, we will create a date time variable by converting from a string:
    QString dateTimeAString = "2016-05-04 12:24:00";
    QDateTime dateTimeA = QDateTime::fromString(dateTimeAString, "yyyy-MM-dd hh:mm:ss");
    qDebug() << "5) " << dateTimeA;
  8. Subsequently, we can also convert the date time variable back to a string with our own custom format:
    QDateTime dateTimeB = QDateTime::currentDateTime();
    QString dateTimeBString = dateTimeB.toString("dd/MM/yy hh:mm");
    qDebug() << "6) " << dateTimeBString;
  9. We can call the QString::toUpper() function to convert a string variable to all capital letters:
    QString hello1 = "hello world!";
    qDebug() << "7) " << hello1.toUpper();
  10. On the other hand, calling QString::toLower() will convert the string to all lowercase:
    QString hello2 = "HELLO WORLD!";
    qDebug() << "8) " << hello2.toLower();
  11. The 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;
  12. This demonstrates how a single 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;

    The full source code in main.cpp will now look like this:

    How to do it…
    How to do it…
  13. Compile and run the project now and you should see something like this:
    How to do it…

How it works...

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.

There's more…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.244.217