QT #1 : Introduction to QT C++

QT(pronounced as Cute) has been around for a long time now. At the time of writing of this blog, Qt 6.4 is the latest stable version.

Why Qt ?

Qt is a cross platoform framework for development of application across Windows, Linux, Android, IOS and embedded devices. Cross Platform application development allows developers to use the same code base for different platform. You could now develop an application in Windows and port of the source code to a Linux machine and compile it. The compile executable would run as if it was natively build for Linux.

Qt supports various programming languages including C++, Python and Qml.

If you are starting with QT, the first obvious step would be to head over to the official QT page and downlod the QT Installer. To begin with, I would prefer the Open Source Edition, but if you are willing to pay for a licensed version, do not hesitate. The installation procedure is quite straightforward and is out of the scope of this tutorial.

Qt Creator

Once you have installed QT, you can start the QT Creator, which is a free cross-platform IDE, with integrated support for C++. Javascript and QML. The IDE features code completion, syntax highlighting, refactoring and has built-in documentation. QT Creator allows you to switch between different target operating systems with easy built settings and has support for CMake, QMake tools.

It also has inbuilt support for integration with popular version controls like Git, Subversion, Perforce and Mercurial.

Selecting Build System

One of the first steps towards building your application is select the build system of your choice. The Qt Creator’s Project Wizard allows you to select any of the following build system

  • qMake
  • CMake
  • Qbs

In this example, we will use qMake. qMake is a built system tool shipped with the Qt library, which automates and simplifies the build process across different platform. The Qt Creator has provided inbuilt support for qMake since the beginging and has more integrated support in Qt Creator, compared to CMake and Qbs. Of course, this could change over time and the support for other built systems could improve over the new releases.

We will explore more on qMake and its syntax as we build our applications and explore more on Qt.

Types of Projects

If you select New Project in Qt Creator, you have different templates to choose from.

Project Types

The key projects types include

  • QT Widget Application
  • Qt Console Application
  • Qt Quick Application

I will leave out other types of project out of the context of this blog post to reduce complixity. We will revisit the topic as we progress along.

Project Wizard

Alright, enough of details !! Let us now get our hands dirty. We will begin with the obligatory “Hello World” application. For the starters, we will begin with a console application even though Qt is a GUI framework. It would give us a chance to keep it as simple as possible and explore a bit of qMake before we start exploring Qt’s powerful libraries.

Use the Create Project button in the QT Creator IDE and select a new QT Console Application.

The project wizard will guide you in selecting the various configurations for the project including the build system, source code location and integration with source repositories.

Select the Location for the source files.

Select Build System. For this example, we have chosen qMake.

If you are aiming a multi-lingual application, select the translation file. For this example, we will choose to skip the translation file.

The Kit selection allows you to choose the platforms you would be targeting the application. For our hello world, we will choose only Desktop.

Finally, the Project Wizard would allow you to select the version control system you would like to associate with the project.

Now that we have our project files created using the Project Wizard, let us now examine the different files that were automatically created for us.

qMake File

There are mainly 2 files that are created for us. The first one is the project file (qmake), usually with the file extension .pro.

We had previously selected qMake as our build system. qMake specification is written in .pro files. Let us breifly examine the project file and it its content.

QT -= gui

CONFIG += c++17 console
CONFIG -= app_bundle

SOURCES += \
        main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target        

The first line, QT -= gui tells the compiler that the application is not an GUI application. We then continue to configure the application using the CONFIG key. In this example, we have set to use C++17 and use console for default output stream.

The third part consists of the files in the project, denoted with the SOURCES key. In this example, we have only a single file named main.cpp in our project.

Let us skip the deployment rules for now. We will explore it detail later. For now, it would be sufficient to understand that these rules guides the deployment process.

main.cpp

We will write our hello world message to the console using C++ in main.cpp. The key entry point to application is the main function defined in main.cpp.

#include <QCoreApplication>
#include <QtDebug>
#include <iostream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    std::cout << "Hello World using std:out" << std::flush;  // Print using std library
    qInfo() << "Hello World Demo";   // Print using Qt
    return a.exec();
}

The first line in entry point function defines a variable of type QCoreApplication. The QCoreApplication is a QT library function defined in in QCoreApplication, which provides a event loop for the non-GUI application.

For GUI application we use QGuiApplication.

The QCoreApplication provides the main event loop for console application so that all the events from the Operating System and other sources can be processed. The event loop is started with QCoreApplication.exec().

For this example, we use two ways to print out our “Hello World” message – using the C++ standard libraries (std::cout) and via the QT libraries (qInfo). QT provides global macros for writing out debug information, including qInfoqDebugqWarningqCritical, and qFatal.

With this code place, you can now run the application and see your first QT application display the messages on console.

That’s all for now. In the next part of this series, we will explore more QT functions, including GUI applications and Widgets. Until then, Happy Coding.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s