MainWindow structure
This generated class is a perfect yet simple example of Qt framework usage; we will dissect it together. As mentioned previously, the MainWindow.ui
file describes your UI design and MainWindow.h
/ MainWindow.cpp
is the C++ object where you can manipulate the UI with code.
It is important to take a look at the header file MainWindow.h
. Our MainWindow
object inherits from Qt's QMainWindow
class:
#include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; };
As our class inherits from the QMainWindow
class, on top of the header file, we add the corresponding include. The second part is the forward declaration of the Ui::MainWindow
, as we only declare a pointer.
The Q_OBJECT
can look a little strange to a non-Qt developer. This macro allows the class to define its own signals/slots and more globally Qt's meta-object system. These features will be covered later in this chapter.
This class defines a public constructor and destructor. The latter is pretty common. But the constructor takes a parameter parent. This parameter is a QWidget
pointer that is null
by default.
A QWidget
is a UI component. It can be a label, a textbox, a button, and so on. If you define a parent-child relationship between your window, layout, and other UI widgets, memory management of your application will be easier. Indeed, in this case, deleting the parent is enough because its destructor will take care of also deleting its child, which in turn will delete its children and so on.
Our MainWindow
class extends QMainWindow
from the Qt framework. We have a ui
member variable in the private fields. The type is a pointer of Ui::MainWindow
, which is defined in the ui_MainWindow.h
file generated by Qt. It's the C++ transcription of the UI design file MainWindow.ui
. The ui
member variable will allow you to interact with your UI components (QLabel
, QPushButton
, and so on) from C++, as shown in the following figure:
Tip
C++ tip
If your class only uses pointers or references for a class type, you can avoid including the header by using forward declaration. That will drastically reduce compilation time.
Now that the header part is done, we can talk about the MainWindow.cpp
source file.
In the following code snippet, the first include is our class header. The second one is the include required by the generated class Ui::MainWindow
. This include is required as we only use a forward declaration in the header:
#include "MainWindow.h" #include "ui_MainWindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this);
In many cases, Qt generates a good piece of code using the initializer list. The parent
argument is used to call the superclass constructor QMainWindow
. Our private member variable ui
is also initialized now.
Now that ui
is initialized, we must call the setupUi
function to initialize all widgets used by the MainWindow.ui
design file:
As we initialize a pointer in the constructor, it must be cleaned in the destructor:
MainWindow::~MainWindow() { delete ui; }