Install Qt Creator and compile your first program on Ubuntu

about compile your first program with Qt Creator

In the next article we are going to take a look at how to install Qt Creator and how to compile a basic program in Ubuntu. Qt is an object-oriented, cross-platform working environment. It is used to develop programs that use a graphical user interface or different command line tools and consoles for servers that do not require a graphical user interface.

Qt is a free, open source, cross-platform application development framework. Is compatible with various platforms as; Gnu / Linux, OS X, Windows, Android, iOS and others. More than a programming language, Qt is a framework written in C ++.

In the following lines we will see how install Qt Creator via the command line. Later we will see how to write a simple style program Hello World puedo cambiarlo and how to run it. All this I am going to do on Ubuntu 19.04.

Install Qt Creator

about QTCreator
Related article:
Qt 5.9.1, (with Qt Creator 4.3.1 included) installation on Ubuntu

To start we will install Build Essential, if you don't already have it installed. This is a package that will allow users to install and use c ++ tools in Ubuntu. To proceed with the installation, we open a terminal (Ctrl + Alt + T) and first we will update the available software and then install the package by typing:

sudo apt update; sudo apt install build-essential

If you don't have the Qt Creator package installed that contains the UI and command line tools for creating and running the Qt project, write in the same terminal:

Qt Creator installation

sudo apt install qtcreator

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

Qt5-default installation

sudo apt install qt5-default

To implement more complex projects, you will have to install Qt documentation and samples. This can be done by typing in the terminal:

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

The package qt5-doc Contains the Qt 5 API documentation. qtbase5-examples contains the Qt Base 5 examples and qtbase5-doc-html Contains the HTML documentation for the Qt 5 base libraries.

Write and compile your first Qt program from the terminal

Everything that is going to be read below can also be done from a graphical environment, but in this case we will do it via the terminal. Writing and compiling Qt programs from the Ubuntu command line is pretty straightforward. The steps to follow are:

Create a project directory

We will start by creating a working directory for host our Qt project. In the terminal (Ctrl + Alt + T) we write:

Qt project directory

mkdir ProyectoQt

Create a .cpp file in the project directory

Inside the directory that we just created, we are going to execute the following command to create a .cpp file:

vim mainEjemplo.cpp

In the blank file, just copy the following code. Its only purpose is to print a line in a window with the title 'My first Qt program'.

code for the Qt example

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

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

QApplication app(argc, argv);
QLabel hola("<center>Ejemplo Qt para Ubunlog</center>");
hola.setWindowTitle("Mi primer programa Qt");
hola.resize(600, 400);
hola.show();
return app.exec();

}

Once pasted into the file, it only remains to save and close the editor.

Create the Qt project file

After saving the file and inside the project folder, the following command will have to be executed to create a Qt project file:

qmake -project

qmake project Qt

We can verify that the previous command has created a file with the name of ProjectQt.pro in the project directory. This project file is the basic skeleton for Qt projects and can be edited.

Settings in the Qt project file

Since we want to compile the project from the command line, we will have to add the following line to the .pro file that we created in the previous step.

QT += gui widgets

To do this, there is only open the SampleProject.pro file and paste inside the previous line:

Qt project file configuration

vim ProyectoQt.pro

Create an executable file for the project

The following command will create a file with the name of 'makefile' in the project directory.

qmake ProyectoQt.pro

build makefile project Qt

Now, we will use make to compile the Makefile in an executable program:

make

As long as there are no errors in the project, this command should create an executable program in the working directory.

creating the executable file

The file ProjectQt in green color, it is the executable file Qt.

Test the program

If you have reached this point, you are all set to launch the executable file that we created in the previous step. You can launch the program by typing in the terminal, inside the project folder:

./ProyectoQt

The output of this example is as follows:

running the sample Qt program

This is the basics that any user should know to write, compile, and run a first Qt program. As I mentioned lines above, you can choose between the command line or the UI method, depending on the needs of your project and personal preferences. For more information, consult the official Qt documentation.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Angel said

    A scope, what the article intends is to install and compile a program using QT, on the other hand Qt Creator is a multiplatform IDE, otherwise it is great