“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.
Allows you to run software written with its help in most modern operating systems by simply compiling a program for each OS without changing the source code. It includes all the basic classes that may be required when developing application software, ranging from GUI elements to 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 how to write a simple program “Hello, World!” Using the Qt4 library
First, let's decide on the development environment. Personally, I use the cross-platform IDE Code :: Blocks to write the program (for more details on working in this IDE with Qt4, read here ). There are also plugins for working with Qt in Eclipse. 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 Qt programs manually.
First, create a file in any text editor and name it, for example, main.cpp.
We write the following in it:
In lines 1 and 2, we included the Qt header files in which the main classes are located.
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 in order) 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 the application object. We pass command line variables to this object.
In line 6 we create a dialog - a graphical window of a rectangular shape, with a title and buttons in the upper right corner. Create a label (line 7). When creating a label, we pass it to the 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 we set the label inscription by calling the setText () function (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, on line 10, we start the application’s event handling cycle for the operating system. We return the result of the object as the result of the program.
Now compile the written program.
We will go to the directory where we saved our main.cpp file and execute the command.
In this case, the Qt4 project blank will be created, which will automatically include all source code files in this directory. The result is a file with a name like the current directory and the extension .pro. It will look like this: As you can see, the source file was added automatically. Run the command As a result, we get a Makefile, which we use to compile the program by executing the following command: Wait until the compilation process is complete and run our first program. It will look something like this:
To get full 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 will be placed in the header file mydialog.h:
In the sixth line, we defined our class as derived from QDialog.
On the next line, we indicated the Q_OBJECT macro, which tells the Qt preprocessor that this class will use additional Qt features, for example, a system of signals and slots.
On line 9, we indicate the constructor of our dialog box. It has only one input parameter - a pointer to the parent object (0 if there is no parent).
We will define the constructor of our class in the mydialog.cpp file:
In line 4, we create a layout manager that will automatically display all widgets added to it vertically. Creating a label 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 he automatically arranges them.
In line 11, we connect the clicked signal (click) 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 communicate between program elements.
The main.cpp file will take the following form:
We recreate the project with a team so
that new files are automatically added to it and compile it. This is what our new program looks like:
If the dialog box contains many graphic elements, then creating such windows is rather tedious. To simplify this process, there is a tool called Qt Designer. We start it
and choose the creation of 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 signal of clicking clicked () of the button button to the close () slot of the dialog box. We arrange them vertically using the layout manager. Save the resulting file as mydialog.ui. Later it will be automatically converted to a header file named ui_mydialog.h.
We modify the header file of our mydialog.h dialog box as follows:
All header files in it are replaced with "ui_mydialog.h", and inheritance becomes multiple.
The constructor is greatly simplified:
The setupUi function is defined in the ui_mydialog.h header file and takes care of the whole routine of creating the form.
The main.cpp file has not changed compared to the second program.
We recreate the project so that new files are automatically added to it and compile it.
This article showed the basic principles of C ++ programming using Qt4. If the habrasociety likes this publication, then I will continue the series of publications on the use of Qt4.
Allows you to run software written with its help in most modern operating systems by simply compiling a program for each OS without changing the source code. It includes all the basic classes that may be required when developing application software, ranging from GUI elements to 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 how to write a simple program “Hello, World!” Using the Qt4 library
Development environment
First, let's decide on the development environment. Personally, I use the cross-platform IDE Code :: Blocks to write the program (for more details on working in this IDE with Qt4, read here ). There are also plugins for working with Qt in Eclipse. 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 Qt programs manually.
First program
First, create a file in any text editor and name it, for example, main.cpp.
We 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.
In lines 1 and 2, we included the Qt header files in which the main classes are located.
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 in order) 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 the application object. We pass command line variables to this object.
In line 6 we create a dialog - a graphical window of a rectangular shape, with a title and buttons in the upper right corner. Create a label (line 7). When creating a label, we pass it to the 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 we set the label inscription by calling the setText () function (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, on line 10, we start the application’s event handling cycle for the operating system. We return the result of the object as the result of the program.
Compilation
Now compile the written program.
We will go to the directory where we saved our main.cpp file and execute the command.
$ qmake -project
In this case, the Qt4 project blank will be created, which will automatically include all source code files in this directory. The result is a file with a name like the current directory and the extension .pro. It will look like this: As you can see, the source file was added automatically. Run the command As a result, we get a Makefile, which we use to compile the program by executing the following command: Wait until the compilation process is complete and run our first program. It will look something like this:
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
SOURCES += main.cpp
$ qmake
$ make
Second program
To get full 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 will be placed in the header file mydialog.h:
- #include <QDialog>
- #include <QPushButton>
- #include <QLabel>
- #include <QVBoxLayout>
-
- class MyDialog : public QDialog {
- Q_OBJECT
- public:
- MyDialog(QWidget *parent = 0);
- };
* This source code was highlighted with Source Code Highlighter.
In the first four lines, we include the necessary header files for the graphic elements used - a dialog, a button, an inscription and a 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 derived from QDialog.
On the next line, we indicated the Q_OBJECT macro, which tells the Qt preprocessor that this class will use additional Qt features, for example, a system of signals and slots.
On line 9, we indicate the constructor of our dialog box. It has only one input parameter - a pointer to the parent object (0 if there is no parent).
We will define the constructor of our class in the mydialog.cpp file:
- #include "mydialog.h"
-
- MyDialog::MyDialog(QWidget *parent) : QDialog(parent) {
- QVBoxLayout *layout = new QVBoxLayout(this);
- QLabel *label = new QLabel(this);
- label->setText("<font color=red>Hello, World!</font>");
- QPushButton *button = new QPushButton(this);
- button->setText("Close");
- layout->addWidget(label);
- layout->addWidget(button);
- connect(button, SIGNAL(clicked()), this, SLOT(close()));
- }
* 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 a label 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 he automatically arranges them.
In line 11, we connect the clicked signal (click) 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 communicate between program elements.
The main.cpp file will take the following form:
- #include <QApplication>
- #include "mydialog.h"
-
- int main(int argc, char* argv[]) {
- QApplication app(argc, argv);
- MyDialog *dialog = new MyDialog;
- dialog->show();
- return app.exec();
- }
* This source code was highlighted with Source Code Highlighter.
We recreate the project with a team so
$ qmake -project
that new files are automatically added to it and compile it. This is what our new program looks like:
Third program
If the dialog box contains many graphic elements, then creating such windows is rather tedious. To simplify this process, there is a tool called Qt Designer. We start it
$ designer
and choose the creation of 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 signal of clicking clicked () of the button button to the close () slot of the dialog box. We arrange them vertically using the layout manager. Save the resulting file as mydialog.ui. Later it will be automatically converted to a header file named ui_mydialog.h.
We modify the header file of our mydialog.h dialog box as follows:
- #include "ui_mydialog.h"
-
- class MyDialog : public QDialog, public Ui::Dialog {
- Q_OBJECT
- public:
- MyDialog(QWidget *parent = 0);
- };
* 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 greatly simplified:
- #include "mydialog.h"
-
- MyDialog::MyDialog(QWidget *parent) : QDialog(parent) {
- setupUi(this);
- }
* This source code was highlighted with Source Code Highlighter.
The setupUi function is defined in the ui_mydialog.h header file and takes care of the whole routine of creating the form.
The main.cpp file has not changed compared to the second program.
We recreate 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 habrasociety likes this publication, then I will continue the series of publications on the use of Qt4.