Let's learn how to convert various data types in Qt by following these steps:
- Open Qt Creator and create a new Qt Console Application project by going to File| New File or Project:
- Open main.cpp and add the following headers to it:
#include <QCoreApplication>
#include <QDebug>
#include <QtMath>
#include <QDateTime>
#include <QTextCodec>
#include <iostream>
- In the main() function, add the following code to convert a string into a number:
int numberA = 2;
QString numberB = "5";
qDebug() << "1) " << "2 + 5 =" << numberA + numberB.toInt();
- Convert a number back into a string:
float numberC = 10.25;
float numberD = 2;
QString result = QString::number(numberC * numberD);
qDebug() << "2) " << "10.25 * 2 =" << result;
- Let's see how to round down a value using qFloor():
float numberE = 10.3;
float numberF = qFloor(numberE);
qDebug() << "3) " << "Floor of 10.3 is" << numberF;
- 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;
- Create a date-time variable by converting from a piece of date-time data written in string format:
QString dateTimeAString = "2016-05-04 12:24:00";
QDateTime dateTimeA = QDateTime::fromString(dateTimeAString, "yyyy-MM-dd hh:mm:ss");
qDebug() << "5) " << dateTimeA;
- Convert the date-time variable back into a string with our custom format:
QDateTime dateTimeB = QDateTime::currentDateTime();
QString dateTimeBString = dateTimeB.toString("dd/MM/yy hh:mm");
qDebug() << "6) " << dateTimeBString;
- Call the QString::toUpper() function to convert a string variable into all capital letters:
QString hello1 = "hello world!";
qDebug() << "7) " << hello1.toUpper();
- Calling QString::toLower() will convert the string completely into lowercase:
QString hello2 = "HELLO WORLD!";
qDebug() << "8) " << hello2.toLower();
- The QVariant class provided by Qt is a very powerful data type that can be easily converted to other types without any effort from the programmer:
QVariant aNumber = QVariant(3.14159);
double aResult = 12.5 * aNumber.toDouble();
qDebug() << "9) 12.5 * 3.14159 =" << aResult;
- This demonstrates how a single QVariant variable can be simultaneously converted to multiple data types without any effort from 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:
#include <QCoreApplication>
#include <QDebug>
#include <QtMath>
#include <QDateTime>
#include <QTextCodec>
#include <iostream>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
- After that, let's add in the code to convert a string into a number, and vice versa:
// String to number
int numberA = 2;
QString numberB = "5";
qDebug() << "1) " << "2 + 5 =" << numberA + numberB.toInt();
// Number to string
float numberC = 10.25;
float numberD = 2;
QString result = QString::number(numberC * numberD);
qDebug() << "2) " << "10.25 * 2 =" << result;
- Write the code to convert floating-point numbers to their nearest succeeding or preceding integer number, respectively:
// Floor
float numberE = 10.3;
float numberF = qFloor(numberE);
qDebug() << "3) " << "Floor of 10.3 is" << numberF;
// Ceil
float numberG = 10.3;
float numberH = qCeil(numberG);
qDebug() << "4) " << "Ceil of 10.3 is" << numberH;
- Convert a string into date-time format, and vice versa:
// Date time from string
QString dateTimeAString = "2016-05-04 12:24:00";
QDateTime dateTimeA = QDateTime::fromString(dateTimeAString, "yyyy-MM-dd hh:mm:ss");
qDebug() << "5) " << dateTimeA;
// Date time to string
QDateTime dateTimeB = QDateTime::currentDateTime();
QString dateTimeBString = dateTimeB.toString("dd/MM/yy hh:mm");
qDebug() << "6) " << dateTimeBString;
- Continue to add code to convert strings into uppercase or lowercase characters:
// String to all uppercase
QString hello1 = "hello world!";
qDebug() << "7) " << hello1.toUpper();
// String to all lowercase
QString hello2 = "HELLO WORLD!";
qDebug() << "8) " << hello2.toLower();
- Convert the QVariant data type into other types:
// QVariant to double
QVariant aNumber = QVariant(3.14159);
double aResult = 12.5 * aNumber.toDouble();
qDebug() << "9) 12.5 * 3.14159 =" << aResult;
// QVariant different types
qDebug() << "10) ";
QVariant myData = QVariant(10);
qDebug() << myData;
myData = myData.toFloat() / 2.135;
qDebug() << myData;
myData = true;
qDebug() << myData;
- Convert the QVariant data type into QDateTime and QString:
myData = QDateTime::currentDateTime();
qDebug() << myData;
myData = "Good bye!";
qDebug() << myData;
return a.exec();
}
- Compile and run the project and you should see something like this: