Detailed overview, configuration and installation of Qt Creator

A question arose on the forum about how to create a dynamic library and correctly connect it to a third-party project. Such questions arise from time to time, so let’s consider one option: creating a dynamic dll library for Windows using standard wizards in Qt Creator.

In this case, we will not consider the option when the project is divided into subprojects, which are compiled as libraries and then included in the main project. Because these will be dynamic internal project libraries. Let's create an external library, which theoretically can be distributed in the form of binaries.

Let's create two projects:

  1. QuiLib
    - this will be an external dynamic library that will contain one dialog box. This dialog box will open in the main project.
  2. WithDynamicLibrary
    - a project that will be used just to connect this dynamic library.

A simple example for manual assembly

For a better understanding of Qt, it would be correct to manually build at least a simple example in the console. The assembly technique is the same for projects of any size and it is important to understand how it is done.

Let's look at a simple example of a GUI application that opens an empty window on the screen. Let's select a directory on the disk where we will create the file main.cpp

with the following content.
Line numbering has been added to the example to simplify further comments on the example. It should be understood that line numbering should not be included in the working file, since it is not part of the valid C++ expression syntax. 01. #include 02. #include 03. 04. int main(int argc, char *argv[]) 05. { 06. QApplication app(argc, argv);
07. QWidget wgt; 08. wgt.setWindowTitle(tr("Hello world")); 09. 10. wgt.show(); 11. return app.exec(); 12. } Lines 01 and 02 include the QApplication

and
QWidget
, which, among other things, contain a declaration of
the QApplication
and
QWidget
. In our example, we create instances of these classes.

It is worth noting the convenient tradition of naming header files used in Qt. Since version 4 of Qt, if you needed a class named Qxxx

, then most likely its definition is in the
Qxxx
.

In this example, the QApplication

interests us as the organizer of the cycle of collecting messages that will arrive in the window of our GUI application.
See line 11 (starting the window message loop). The QApplication
class is implemented like a Singleton.
For those who are not familiar with this term from the theory of design patterns (or, patterns, from the English patterns
), we will give a short explanation.
The essence of a singleton is that the implementation of a class prevents the possibility of creating more than one instance of a given class. This feature is important to us because it implies the possibility of defining a global variable inside the Qt library with a pointer to a single instance of a given class. See the description for the qApp
in the Qt help.

The Qt library uses widget terminology (from the English widget

- contraption, device) as an element of the GUI interface.
The minimum common set of properties for such elements is represented by the QWidget
.

Lines 06 and 07 create the application and widget class objects. In both cases, objects are created statically. The lifetime of such objects is limited by the statement block { ... } in which they were created. As soon as the execution of the program code reaches the closing bracket, both of these objects are automatically destroyed. Thus, in this example we will not think about how to destroy Qt objects and will look at this issue later.

When creating a QApplication

a parameterized constructor is used to which the application launch arguments are passed (copied from the main() function arguments).
Inside the class object, these arguments are parsed. The application class supports a number of launch options, which can be found in the help for the corresponding QApplication
.
Launch parameters not included in this list should be analyzed independently. At the end of this lesson, having placed several controls in the main application window, we will suggest experimenting with the -style launch parameter, for which, in any Qt build, the possible values ​​are: motif
,
windows
,
platinum
.

Line 08 changes one of the widget object's attributes. Using the setWindowTitle() method, we set our own text for the title of our future window. Note that the string image is wrapped in a call to the translation function tr()

. This is a requirement to internationalize the application. For all such wrappers, special Qt tools can be used to create special files with translations into different languages, which can be used in the application to perform automatic replacements. Typically, such files are included in the application build as resources.

Line 10 opens the application window. Before Qt4, the widget was explicitly declared as the main application widget before opening the window. This was done using the following code.

app.setMainWidget(&wgt);

Starting from Qt4 version, such communication is performed automatically by accessing the qApp

to an instance of the application class.

Line 11 starts the operating system message processing loop directed to the application window. The loop ends when any of the application closing commands are executed. The application exit code is returned by exec() when the method exits. It is this code that becomes the return code of the main() function by passing it through the return

.

Now let's try to build the application. The easiest way to do this is on Linux. To do this, you just need to open the console and run some commands, which we will now describe. For Windows, such work may require setting paths to the directory where the qmake utility from the Qt SDK is located. This utility implements the rules of the QMake project build system.

First we need to find out what is available to us from the console. If we are in the bash (*nix) console, then this is quite simple. Type qmake

and press tab twice.
We should see a list of all commands that start with qmake
.
For example, in my case I see two commands: qmake
and
qmake-qt4
.
This means that I have two versions of the library installed from the repository. qmake
command corresponds to the Qt5 version (the default is for the latest version), and the
qmake-qt4
corresponds to Qt4. Now, depending on which command I use, I will either build using the Qt5 version or using the Qt4 version.

If everything in our system is configured normally and the Qt SDK versions are successful, then the project should be built using the following three commands.

$ qmake -project $ qmake $ make

The first command will create a project file. The project file has the suffix .pro

.
In the context of Windows, it would be correct to say “ pro
”.
The concepts of file name suffix
and
file extension
mean completely different things, although they seem similar. Make sure to use it correctly.

The second command should create a compilation script file - Makefile. The third command should run a compilation script that should produce an executable application file.

If this does not happen, then we will try to find the problem.

Open the project file. Try to find the following line there.

QT += gui

If such a line is not there, then you should add it, otherwise, depending on the SDK version, the project may be perceived as a console project and the paths to the GUI class header files will not be included in it. This will result in compilation errors stating that included header files were not found.

It should be kept in mind that if you are dealing with Qt SDK version 5, then this definition must also include the widgets

as shown below.
QT += gui widgets

Installing Qt Creator components

If it suddenly happens that you forgot to install a component, or, conversely, want to remove it, then the Qt Maintenance Tool will come to the rescue. This is a tool that allows you to manage all components of Qt Creator.

To run it, go to the applications menu, select "Development" -> "Qt Maintenance Tool" .

Select the required option (Remove/add components, update components or remove Qt). Then perform the necessary operations and close the window.

conclusions

Installation and configuration of Qt Creator is complete. Now you can create your programs for a huge number of platforms, leaving the code untouched! By the way, by installing Qt on Windows, you can compile this project there too. Good luck to you!

Related posts:

  • Installing Intellij IDEA on ubuntu 18.04

    December 16, 2016

  • Installing Go Ubuntu

    May 12, 2017

  • Installing Composer Ubuntu 18.04

    May 9, 2017

  • Installing Ruby on Ubuntu

    June 12, 2017

Setting up Qt Creator

After installation is complete, restart your computer and launch Qt Creator. Go to menu "Tools" -> "Options" .

There are several tabs to consider here.

1. Environment is the setting of the appearance of the IDE itself, as well as changing keyboard shortcuts and managing external utilities.

2. Text editor - here you can customize the appearance, fonts and colors of the editor.

3. C++ - syntax highlighting, working with file extensions and UI (i.e. forms).

4. Android - paths to the necessary tools are collected here, and connected or virtual devices are also configured in this menu.

Working with Qt Creator - first project

Well, the hour has struck! The installation of Qt Creator is complete. It's time to make your first cross-platform application on Linux and then compile it on Windows. Let it be... a program that displays a Qt icon, a button and an inscription, on which a random phrase will be displayed when the button is pressed. The project is simple, and, of course, cross-platform!

First, let's open the development environment. Click “File” -> “Create a file or project...” . Let's choose the Qt Widgets application - it's quick and convenient to create. And its name is “Cross-Platphorm” . That's how it is!

Set - default. We also leave the main window unchanged. Let's create a project.

First you need to configure the form - the main window of the application. It's empty by default, but it won't stay that way for long.

Let's go to the folder "Forms" -> "mainwindow.ui" . The Qt Designer window will open:

Remove the menu bar and toolbar from the form by right-clicking and selecting the appropriate item. Now drag the Graphics View, Push Button and Label elements like this:

To change the text, double-click on the element. In the Label properties (on the right), select the text position vertically and horizontally - vertical.

Now it's time to figure out the output of the icon. Let's go to the editor, right-click on any folder on the left and select "Add new..." . Now click “Qt” -> “Qt Resource File” . Name - res. In the window that opens, click “Add” -> “Add prefix” , and after adding - “Add files” . Select the file, and in the “Incorrect file location” click “Copy” .

Happened! We save everything. Open the form again. Right-click on Graphics View, select “styleSheet...” -> “Add resource” -> “background-image” . In the left part of the window that appears, select prefix1, and in the right - our picture. Click "OK" . Adjust the length and width.

All! Now you can start coding. Right-clicking on the button opens a context menu, now you need to click “Go to slot...” -> “clicked()” . In the window we enter the following code:

Or you can download the full project on GitHub. Work with Qt Creator is completed, click on the green arrow icon on the left, and wait for the program to launch (if the arrow is gray, first click on the hammer icon). It started! Hooray!

Rating
( 1 rating, average 4 out of 5 )
Did you like the article? Share with friends:
For any suggestions regarding the site: [email protected]
Для любых предложений по сайту: [email protected]