If you create a Qt widget application with class name MainWindow, Qt Creator will give you this template header:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
and this template source:
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
What is ui? What is its relationship to this?
The qt program
uiccompiles the.uifile to a.hfile. It makes a class that holds pointers to all the widgets defined in the form, and a member functionsetupUiwhich constructs all those widgets, setting the properties specified in the form, with the top-level widget being a direct child of the passed in widget (yourMainWindowinstance in this case). It doesn't automatically show any window, other code calls theshowmember of your main window.If you create another
Ui::MainWindowand call it'ssetupUi, you will populate another widget with all the descendant widgets described in the form.