How to do it...

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

  1. Open Qt Creator and create a new Qt Console Application project by going to File| New File or Project:
  1. Open main.cpp and add the following headers to it:
#include <QCoreApplication>#include <QDebug>#include <QtMath>#include <QDateTime>#include <QTextCodec>#include <iostream>
  1. 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();
  1. Convert a number back into a string:
float numberC = 10.25;float numberD = 2;QString result ...

Get Qt5 C++ GUI Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.