Making the first program with Qt library and C++

Qt is one of the most robust cross-platform application development framework. Some well known application developed with this is VLC player, google earth, Skype and Maya. It was first released in May 1995 and is dual licensed, which means it can be used for creating open source applications as well as commercial ones. Qt comes with Qt toolkit which is a very powerful utility for development of applications. Large number of open source developers use Qt all over the world on various projects.

In this tutorial, it is assumed that the reader is familiar with the fundamentals of C++ and Linux Commands.


Installing Qt in Debian Based Linux

I am not covering the installation of Qt on other operating systems as it is well documented in Qt’s main website. However, for ease of use in debian based linux system, I will go ahead and explain on how to install Qt in your debian based linux system. Since we are talking about debian based linux, we will be using the apt command for installation of Qt.

The command for installing Qt in your linux system through your terminal is:

$ sudo apt install qt5-default

This will install all the necessary packages required for our simple work of making a Qt based C++ program. At the time of writing this article, qt5 was used, hence the package name is qt5-default.


Making our first Qt based C++ program

To start off with the Qt Based C++ program we will going make a simple console based program that will only print out the version of Qt it used while compilation. We will name this program source code file as version.cpp. Also, we will keep this file in a separate (and empty) folder named “version”. Here is that source code of the that file:

#include <QtCore>
#include <iostream>

using namespace std;

int main(){
	cout << "Qt Version : " << qVersion() << endl;
	return 0;
}

The above code is self explanatory. QtCore is a class based library of Qt whose function named qVersion() is called in the main() function of the program which gives the version number of the current Qt.

In order to compile this program properly, we need to make it into a project format that will build all the necessary configuration files for compilation. To make this into a project format simply go in your terminal then, go to the directory where you have placed your source file and put this command:

$ qmake -project

The above command will generate one file with .pro extension. The project files contains the details of projects such as what should be the template name, what should be the target name for the compilation of the binary file, and some other configurations such as which API to disable or not.

Next, we will have to generate a Makefile so that the compiler could finally compile the program with the instructed Makefile commands. This could be done with the following command:

$ qmake

As we run the above command the qmake tool will generate the Makefile in accordance with the details given in project file generated before.

After this is done, the last step to do is to compile. Since the Makefile was already generated by qmake, we will just have to run the make command to compile the whole project. Keep in mind that, whatever is the folder name in which the source file resides, that will be the binary file’s name after compilation. For me, this folder’s name was “version”.

$ make

After this, you could run the program with its binary file’s name, such as:

$ ./version

It will give an output as such:

Qt Version : 5.9.5

And that is your first Qt based C++ program.