2016-06-27 00:10:37 +03:00
---
2021-03-10 06:43:53 +03:00
category: framework
2024-10-21 00:46:35 +03:00
framework: Qt
language: C++
2016-06-27 00:10:37 +03:00
filename: learnqt.cpp
contributors:
- ["Aleksey Kholovchuk", "https://github.com/vortexxx192"]
2017-08-25 11:18:17 +03:00
2016-06-27 00:10:37 +03:00
---
2016-10-01 21:52:59 +03:00
**Qt** is a widely-known framework for developing cross-platform software that can be run on various software and hardware platforms with little or no change in the code, while having the power and speed of native applications. Though **Qt** was originally written in *C++* , there are its ports to other languages: *[PyQt](https://learnxinyminutes.com/docs/pyqt/)* , *QtRuby* , *PHP-Qt* , etc.
2016-06-27 00:10:37 +03:00
2016-10-01 21:52:59 +03:00
**Qt** is great for creating applications with graphical user interface (GUI). This tutorial is how to do it in *C++* .
2016-06-27 00:10:37 +03:00
```c++
/*
2017-08-23 11:14:39 +03:00
* Let's start classically
2016-06-27 00:10:37 +03:00
*/
// all headers from Qt framework start with capital letter 'Q'
#include <QApplication>
#include <QLineEdit>
int main(int argc, char *argv[]) {
// create an object to manage application-wide resources
QApplication app(argc, argv);
// create line edit widget and show it on screen
QLineEdit lineEdit("Hello world!");
lineEdit.show();
// start the application's event loop
return app.exec();
}
```
GUI-related part of **Qt** is all about *widgets* and *connections* between them.
[READ MORE ABOUT WIDGETS ](http://doc.qt.io/qt-5/qtwidgets-index.html )
```c++
/*
* Let's create a label and a button.
* A label should appear when a button is pressed.
*
* Qt code is speaking for itself.
*/
#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDialog dialogWindow;
dialogWindow.show();
// add vertical layout
QVBoxLayout layout;
dialogWindow.setLayout(&layout);
QLabel textLabel("Thanks for pressing that button");
layout.addWidget(&textLabel);
textLabel.hide();
QPushButton button("Press me");
layout.addWidget(&button);
// show hidden label when the button is pressed
QObject::connect(& button, & QPushButton::pressed,
& textLabel, &QLabel::show);
return app.exec();
}
```
Notice that *QObject::connect* part. This method is used to connect *SIGNALS* of one objects to *SLOTS* of another.
2017-08-23 11:14:39 +03:00
**Signals** are being emitted when certain things happen with objects, like *pressed* signal is emitted when user presses on QPushButton object.
2016-06-27 00:10:37 +03:00
**Slots** are *actions* that might be performed in response to received signals.
2016-10-01 21:52:59 +03:00
[READ MORE ABOUT SLOTS AND SIGNALS ](http://doc.qt.io/qt-5/signalsandslots.html )
2016-06-27 00:10:37 +03:00
Next, let's learn that we can not only use standard widgets but also extend their behaviour using inheritance. Let's create a button and count how many times it was pressed. For this purpose we define our own class *CounterLabel* . It must be declared in separate file because of specific Qt architecture.
```c++
// counterlabel.hpp
#ifndef COUNTERLABEL
#define COUNTERLABEL
#include <QLabel>
class CounterLabel : public QLabel {
Q_OBJECT // Qt-defined macros that must be present in every custom widget
public:
CounterLabel() : counter(0) {
setText("Counter has not been increased yet"); // method of QLabel
}
public slots:
// action that will be called in response to button press
void increaseCounter() {
setText(QString("Counter value: %1").arg(QString::number(++counter)));
}
private:
int counter;
};
#endif // COUNTERLABEL
```
```c++
// main.cpp
// Almost the same as in previous example
#include <QApplication>
#include <QDialog>
#include <QVBoxLayout>
#include <QPushButton>
#include <QString>
#include "counterlabel.hpp"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QDialog dialogWindow;
dialogWindow.show();
QVBoxLayout layout;
dialogWindow.setLayout(&layout);
CounterLabel counterLabel;
layout.addWidget(&counterLabel);
QPushButton button("Push me once more");
layout.addWidget(&button);
QObject::connect(& button, & QPushButton::pressed,
& counterLabel, &CounterLabel::increaseCounter);
return app.exec();
}
```
2016-10-01 21:52:59 +03:00
That's it! Of course, Qt framework is much much larger than the part that was covered in this tutorial, so be ready to read and practice.
2016-06-27 00:10:37 +03:00
2016-10-01 21:52:59 +03:00
## Further reading
2016-06-27 00:10:37 +03:00
2016-10-01 21:52:59 +03:00
- [Qt 4.8 tutorials ](http://doc.qt.io/qt-4.8/tutorials.html )
- [Qt 5 tutorials ](http://doc.qt.io/qt-5/qtexamplesandtutorials.html )
Good luck and have fun!