📜 ⬆️ ⬇️

"Hello, World!" On Qt

Qt is a cross-platform software development toolkit in the C ++ programming language. There are also "bindings" to many other programming languages: Python - PyQt, Ruby - QtRuby, Java - Qt Jambi, PHP - PHP-Qt and others.
It allows you to run software written with it in most modern operating systems by simply compiling the program for each OS without changing the source code. It includes all the main classes that may be required in the development of application software, ranging from GUI elements and ending with classes for working with the network, databases and XML. Qt is fully object-oriented, easily extensible, and supports component programming techniques.
In this article, I will show you how to write a simple “Hello, World!” Program using the Qt4 library.

Development environment


First, we define the development environment. Personally, I use a cross-platform IDE Code :: Blocks for writing a program (for more information about working in this IDE with Qt4, you can read it here ). There are also plugins for working with Qt in Eclipse. The commercial version of Qt for MS Windows can be integrated into MSVS. Programs can also be written in any text editor, and then compiled from the command line.
For clarity, I will show how to compile programs written in Qt manually.

First program


First, in any text editor, create a file and name it, for example, main.cpp
Let's write the following in it:
#include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  1. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  2. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  3. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  4. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  5. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  6. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  7. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  8. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  9. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
  10. #include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .
#include <QtCore> #include <QtGui> int main( int argc, char * argv[]) { QApplication app(argc, argv); QDialog *dialog = new QDialog; QLabel *label = new QLabel(dialog); label->setText( "<font color=red>Hello, World!</font>" ); dialog->show(); return app.exec(); } * This source code was highlighted with Source Code Highlighter .

In lines 1 and 2 we have connected the Qt header files which contain the main classes.
In line 4, we declared the main function — the main function with which the execution of any program begins. It returns an integer (the result of the program; 0 if everything is all right) and takes two variables as input - the number of command line parameters and the array in which they are stored.
In line 5, we create an application object. We are passing command line variables to this object.
In line 6 we create a dialog - a rectangular graphic window, with a title and buttons in the upper right corner. Create a label (line 7). When creating a label, we pass to its constructor a pointer to the dialog, which becomes its parent. When a parent is deleted, all its descendants are automatically deleted, which is very convenient. Then set the label label by calling the function setText () (line 8). As you can see from the example, you can use html-tags for the displayed text.
In line 9, we display our dialog box with a label on the screen.
And finally, in line 10, we start the operating system event cycle by the application. The result of the object, we return as the result of the program.

Compilation


Now compile the written program.
Go to the directory where we saved our main.cpp file and execute the command
')
$ qmake -project

This will create a Qt4 project blank, which will automatically include all the source code files in this directory. The result is a file with the name of the current directory and the extension .pro. It will look like this:

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += main.cpp


As you can see the source file was added automatically. Execute the command

$ qmake

As a result, we get a Makefile, which we use to compile the program by running the following command:

$ make

Let's wait until the compilation process is over and run our first program. It will look something like this:


Second program


To gain complete control over the created windows and other widgets, it is necessary to create classes derived from them. Create a derived class MyDialog. We will use the QDialog class as the parent. The description of our class is placed in the header file mydialog.h:
  1. #include <QDialog>
  2. #include <QPushButton>
  3. #include <QLabel>
  4. #include <QVBoxLayout>
  5. class MyDialog: public QDialog {
  6. Q_OBJECT
  7. public :
  8. MyDialog (QWidget * parent = 0);
  9. };
* This source code was highlighted with Source Code Highlighter .
In the first four lines, we include the necessary header files of the graphic elements used - the dialog, buttons, captions and the vertical layout manager. It is not recommended to use such large header files as <QtGui>, <QtCore>, etc. in large projects, as this increases the compilation time.
In the sixth line, we defined our class as a derivative of QDialog.
On the next line, we specified the Q_OBJECT macro, which indicates to the Qt preprocessor that this class will use additional Qt features, for example, a system of signals and slots.
On line 9, we specify our dialog box designer. It has only one input parameter - a pointer to the parent object (0 if there is no parent).
We define the constructor of our class in the file mydialog.cpp:
  1. #include "mydialog.h"
  2. MyDialog :: MyDialog (QWidget * parent): QDialog (parent) {
  3. QVBoxLayout * layout = new QVBoxLayout ( this );
  4. QLabel * label = new QLabel ( this );
  5. label-> setText ( "<font color = red> Hello, World! </ font>" );
  6. QPushButton * button = new QPushButton ( this );
  7. button-> setText ( "Close" );
  8. layout-> addWidget (label);
  9. layout-> addWidget (button);
  10. connect (button, SIGNAL (clicked ()), this , SLOT (close ()));
  11. }
* This source code was highlighted with Source Code Highlighter .

In line 4, we create a layout manager that will automatically display all widgets added to it vertically. Creating an inscription is similar to the previous example.
In lines 7 and 8 create a button and set its text. On the next two lines, we add our widgets to the layout manager so that it automatically organizes them.
In line 11, we connect the click signal () of the button button to the close () slot of our dialog box. Each Qt object can have its own signals and slots that can be connected to the signals and slots of other objects and thus carry out communication between program elements.
The main.cpp file will look like this:
  1. #include <QApplication>
  2. #include "mydialog.h"
  3. int main ( int argc, char * argv []) {
  4. QApplication app (argc, argv);
  5. MyDialog * dialog = new MyDialog;
  6. dialog-> show ();
  7. return app.exec ();
  8. }
* This source code was highlighted with Source Code Highlighter .

We recreate the project with a team

$ qmake -project

so that new files are automatically added to it and compile it. Here is our new program:


Third program


If the dialog box contains a lot of graphic elements, then creating such windows is quite tiresome. To simplify this process, there is a tool called Qt Designer. Run it

$ designer

and choose to create a dialog box without buttons. Add a label and a button to it, edit their text. Using the Signal / Slot Editor tool, we connect the clicked () signal of the button to the close () slot of the dialog box. Arrange them vertically with the layout manager. Save the resulting file as mydialog.ui. Later it will be automatically converted into a header file named ui_mydialog.h.
We change the header of our dialog mydialog.h as follows:
  1. #include "ui_mydialog.h"
  2. class MyDialog: public QDialog, public Ui :: Dialog {
  3. Q_OBJECT
  4. public :
  5. MyDialog (QWidget * parent = 0);
  6. };
* This source code was highlighted with Source Code Highlighter .

All header files in it are replaced with “ui_mydialog.h”, and inheritance becomes multiple.
The constructor is much simpler:
  1. #include "mydialog.h"
  2. MyDialog :: MyDialog (QWidget * parent): QDialog (parent) {
  3. setupUi ( this );
  4. }
* This source code was highlighted with Source Code Highlighter .

The setupUi function is defined in the ui_mydialog.h header file and takes over the entire routine of form creation.
The main.cpp file has not changed since the second program.
Re-create the project so that new files are automatically added to it and compile it.

Conclusion


This article showed the basic principles of C ++ programming using Qt4. If the habrasoobshchestvu like this publication, then I will continue the cycle of publications on the use of Qt4.

Source: https://habr.com/ru/post/50765/


All Articles