Qt is a free, open source, and cross-platform application development framework for desktop, embedded and mobile. It supports various platforms such as Linux, OS X, Windows, VxWorks, QNX, Android, iOS, BlackBerry, Sailfish OS, and others. More than a programming language. Qt is a framework written in C++. A preprocessor, Meta-Object Compiler is used to extend the C++ language with features like signals and slots. Before the compilation step, the MOC parses the source files written in Qt-extended C++ and generates standard compliant C++ sources from them. Thus the framework itself and applications/libraries using it can be compiled by any standard compliant C++ compiler like Clang, GCC, ICC, MinGW, and MSVC.

In this article, we will first describe how to install the Qt Creator through the command line. Then, we will explain how to write a simple Qt Hello World program and run it through:

  • The Command line
  • The Qt Compiler GUI

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Installing Qt Creator

Let us first start by installing the Build Essential that is a meta package that lets you install and use c++ tools in Ubuntu.

Open the Ubuntu command line, the Terminal, either through the Application Launcher search bar or the Ctrl+Alt +T shortcut.

Then run the following command as sudo in order to update your local repository index with that of the Internet:

$ sudo apt-get update

This command lets you install the latest available software packages available on the Internet.

Now, run the following command as sudo in order to install the Build Essential package:

$ sudo apt-get install build-essential

Install Build Essential meta package

Please note that only an authorized user can add, remove, update, and configure software on Ubuntu. Please provide your password and enter Y when the system prompts you with a choice to continue the installation.

Next, run the following command as sudo in order to install the Qt Creator package that contains both the UI and command line tools for Qt project creation and execution.

$ sudo apt-get install qtcreator

Install QTCreator

Please enter Y when the system prompts you with a choice to continue the installation.Advertisement

If you want Qt 5 to be used as the default Qt Creator version, then run the following command:

$ sudo apt install qt5-default

Install QT5

Please enter Y when the system prompts you with a choice to continue the installation.

For deploying more complex projects later, you will need to install the Qt documentation and examples. You can do so by running the following command as sudo:

$ sudo apt-get install qt5-doc qtbase5-examples qtbase5-doc-html
  • qt5-doc: This package contains the Qt 5 API Documentation.
  • qtbase5-examples: This package contains the Qt Base 5 examples.
  • qtbase5-doc-html: This package contains the HTML documentation for the Qt 5 Base libraries.

Install documentation packages

Please enter Y when the system prompts you with a choice to continue the installation.

You are now ready to use the Qt Creator command line and UI tools!

Your first Qt Program; Written and compiled from the Terminal

A Terminal-savvy person like me is always on a look out for finding solutions for all the programming problems within the Terminal itself. It is after all, very simple to write and compile Qt programs from the Ubuntu command line. Please follow these steps in order to write and execute a simple program Qt program from the Terminal.

Step 1: Create a Project Directory

Let us first create a working directory that will later serve as a project directory for our Qt project.

$ mkdir SampleProject

You can, of course, choose project and file names according to your own wishes. Just be consistent and careful with them as you follow the steps of this tutorial.

Create directory

Step 2: Create a .cpp file in the project directory

Move to the newly created project directory through the following command:

$ cd SampleProject

Then, create a main .cpp file in this directory through the nano editor as follows:

$ nano testmain.cpp

You can, of course, use any of your favorite text editors for this purpose.

Create main cpp file

In the blank file, copy this Qt program whose sole purpose is to print a line of sentence on a window with the title “My First Qt Program”.

#include 
#include 
#include 

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

QApplication app(argc, argv);
QLabel hello("
Welcome to my first Qt program
"); hello.setWindowTitle("My First Qt Program"); hello.resize(400, 400); hello.show(); return app.exec(); }

Tip: You can copy this code by selecting it from here, and then paste it in the file by using the Ctrl+Shift +V shortcut. Alternatively, you can paste the selected text by using the Paste option from the right-click menu in the File.

My first QT program

Quit the file by using the Ctrl+X shortcut and then save it by entering Y, and then hitting Enter.

Step 3: Create the Qt project file

After saving the file, run the following command in order to create a Qt Project file:

Create a project file

$ qmake -project

Running the ls command will display that the above command has created a file by the name of SampleProject.pro in the project directory. This project file is the basic skeleton file for Qt projects and it can be edited for complex purposes.

Step 4: A little configuration to the Qt project file

As we want to compile out project from the command line, we will need to add the following line to our .pro file.

Open the SampleProject.pro file in the nano editor as follows:

$ nano SampleProject.pro

Edit project file

Add the above-mentioned line and Save the file by hitting Ctrl+X, and then by entering Y and hitting Enter.

Step 5: Run qmake to make the project platform-specific

To make the .pro file platform specific, you need to run the following command on the project file:

$ qmake SampleProject.pro

Compile code with qmake

This will create a file by the name of “Makefile” in the project directory. You can verify this by running the ls command, just as I did.

Step 6: Create and Executable Qt File for the project

The following make command will help you in compiling the Makefile into an executable program.

$ make

Run make

Providing that there are no errors in your project, this command should create an executable program in your working directory. You can verify this by running the ls command, just as I did.

The SampleProject file, in green color, is my executable Qt file.

Step 7: Run the Executable file

Finally, we are ready to run the executable file we created in the last step through the Terminal. Run the following command to do so:

$ ./SampleProject

Here is the output of the program:

Start sample QT program

A window with a title and label that we provided in the code!

Your first Qt Program, compiled from the Qt Creator UI

If you are interested in executing the same program from the Qt Creator UI, please follow this procedure:

Launch Qt Creator

Launch the Qt Creator application either through the system Application Launcher search as follows, or directly through the Applications listing.

Launch QTCreator

You can even launch it by entering the following command in your Terminal:

$ qtcreator

Create a Qt Project

The application will open in the following view.

Create a new project by Clicking on the New Project button in the following Projects view:

Create new project in QT creator

This will open the New File or Project view as follows:

New project dialog

We will be choosing a template for our project here. Click on the Application option from the Project panel and select Qt Widgets Application as a template. This template creates a Qt application for the desktop. It includes a Qt Designer-based main window. Click on the Choose button after making your selections; this will open the following Qt Widgets Application view:

QT Widgets application

Enter the name of your Qt project and specify the location where you want to create it. Then click Next; it will open the following view for you to make a Kit Selection:

Kit selection

Make sure that Desktop is selected as the kit, and then click Next. You will then be presented with the Class Information as follows:

Class information

You can, of course, change the class names according to your will but for your first Qt program, we prefer you use the default file names. Click Next and you will be presented with the following Project Management details:

Project management

Click the Finish button and you are ready to work on your first Qt project.

Your First Qt Program

The Qt Creator will open your mainwindow.cpp file by default. Click on the main.cpp file from the Project hierarchy presented in the left-most panel. In the main.cpp file, replace the existing code with the following one:

#include 
#include 
#include 

int main(int argc, char *argv[ ])
{
QApplication app(argc, argv);
QLabel hello("
Welcome to my first Qt program
"); hello.setWindowTitle("My First Qt Program"); hello.resize(400, 400); hello.show(); return app.exec(); }

This is how your project window should look like:

Sample app in QT Creator

Save the file by using the Ctrl+S shortcut or by selecting Save from the File menu.

Running the Program

Finally, Run the project by using the Ctrl+R shortcut or by selecting Run from the Build menu.

Your program will be compiled and run, and you will be able to see the following output:

Start program in QT Creator

A window with our specified title and label! Do not forget to build the project before running it, the next time you make any changes to the code.

So, this was all you needed to know in order to write, compile, and run your first Qt program. You can choose if the command line or the UI method suits you more, depending on your project needs and personal preferences.

Compiling your first Qt Program in Ubuntu