C++

Qt Creator Tutorial

Linux provides its users with various applications and tools for building and developing programs on different operating systems. Many of these applications require components which are graphical and thus Linux provides us with Qt Creator.

Qt Creator is one of the popular application development frameworks available for Linux and other platforms for making graphical user interfaces. It allows users to use GUI as well as command line tools and server consoles for building applications. A lot of famous applications were built using Qt such as VLC(the famous multimedia player), Plasma, and the applications associated with it.

To integrate with the Qt developers and their needs, Qt Creator is available that makes the creation of various Qt applications faster. This tutorial will guide you through the installation as well as how to use Qt Creator for building an application.

How to Install Qt Creator?

The installation of Qt Creator is a fairly easy step as it is available on the main distribution repository so we simply need to update our repository by running the command provided below:

$ sudo apt update

Now install the Qt Creator by running the below command:

$ sudo apt install qtcreator

The  installation process might take some time so be patient. Another thing that you need to install is qmake which makes the project file for Qt Creator and for that execute the below command:

$ sudo apt-get install qt5-qmake

You can also specify a particular version that you want to for example if you want to install Qt Creator 5, you can use the below command:

$ sudo apt install qt5-default

In case you want to create more complex projects you’ve to install the documentation and samples and for that you can use the below command:

$ sudo apt-get install qt5-doc qtbase5-examples qtbase5-doc-html

Now that the Qt Creator is installed you can launch it by searching on your system and the following window will be displayed:

Create a Program with Qt Creator

You can use both GUI as well as command line to create programs in Qt Creator but for this tutorial we will use command line to create a basic program to demonstrate the working.

Firstly you’ve to create a directory to host our Qt project and for that execute the below command:

$ mkdir qtProject

Here we created a directory by the name “qtProject” and later using the command “ls” we verified if the directory is present or not. Now we’ve to move to the directory we created and in that directory we will create another file of extension “.cpp”:

$ cd qtProject
$ nano firstProject.cpp


This command will open up a file where we’ve to write our code and for this purpose paste the below code in the file and save it:

#include <QApplication>
#include <QLabel>
#include <QWidget>

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

QApplication app(argc, argv);
QLabel hello("<center>Qt Example</center>");
hola.setWindowTitle("First QT Project");
hola.resize(600, 400);
hola.show();
return app.exec();

}


Now after saving the file we’ve to create a Qt project file inside the directory and or that you need to execute the below command:

$ qmake -project


You can verify if the file is created by using the command “ls”:


Here the above output shows that the file “qtProject” is created with the extension “.pro” which will be a basic skeleton for the Qt project. Now, to compile the project from the command line we’ve to add the line provided below to the “.pro” file and for this we will first open the .pro file:

$ nano qtProject.pro


This will open the file in which we need to paste the below line:

QT += gui widgets


Now save the file and create an executable file for your project and for that execute the below command:

$ qmake qtPorject.pro

This command will create a file with name “Makefile” which you can verify by using the command “ls”:


Now after this run the below command to compile “MakeFile” in an executable program:

$ make

If no error occurred then you will have an executable program in the directory which you can check using command “ls”:


Here you can see the green colored file “qtProject” is an executable file that has been created in the directory. Now all we’ve to do is test the program that we created and for that run the below command:

$ ./qtProject

This will pop up the window of our program:

This is all the steps you need to follow in order to create a Qt project using the command line. This was a simple example to demonstrate the use of Qt creator but you can create more complex programs.

Conclusion

Qt Creator is a framework that is available for various operating systems to help users develop graphical user interfaces for their program. In this tutorial we guided you through the installation of Qt Creator and further we created a simple program using Qt Creator through the command line and compiled it. Later we executed the program to show you the output. Following these steps you can also create programs that are more complex.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.