Welcome: Hunan Intelligent Applications Tecgnology CO.,ltd.-HNIAT.com
Language: Chinese ∷  English

Basic knowledge

Interpretation of Ui namespace and setupUi() principle

1 Introduction
Selecting GUI applications with the latest QtCreator produces a project with the following files:

1.1 *.pro files
QT += core gui //Use Qt's Core and Gui modules. QT divides its own library functions into multiple modules, the most commonly used are QtCore and QtGui. Additional modules can be added if //other modules are used. Commonly used are QtNetwork, QtOpenGl, QtSql, QtXml. QtWebkit, etc. //If the application uses this module, the corresponding module needs to be added.
TARGET = test0831 //The name of the generated application or link library
TEMPLATE = app //The type of project, generally there are app and lib, app is a direct application, lib is a dynamic link library, generally used for plug-in development
 
SOURCES += main.cpp\ //It is the list of *.cpp files. When displaying multiple lines, use \
        mainwindow.cpp
 
HEADERS += mainwindow.h //*.h file list
 
FORMS += mainwindow.ui //List of UI files, UI files are Qt-specific interface design files
1.2 mainwindow.h file
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
namespace Ui {
    class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_
This file is the class declaration file. It conforms to the idea of interface-oriented programming of mainstream OOP. Generally, the definition of the class is in this file. Operations are generally no longer defined in this file. Note that the internal file inherited from Qt must add the macro definition of Q_OBJECT to the class. Qt will use MOC to recompile the modified file into the moc_mainwindow.cpp file, which is the original C++ class file. Therefore, the built-in class files of Qt are not original C++ classes, and need to be processed by the Qt compiler, so the class that inherits the sub-Qt must add this macro definition, otherwise the compilation will not pass.
Ui::MainWindow* ui variable declaration is the layout class Ui::MainWindow generated by mainwindow.ui.
1.3 mainwindow.cpp file
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
is the class definition file. Note #include "ui_mainwindow.h". The mainwindow.ui file in the project will be compiled by UIC to generate ui_mainwindow.h. The class in the file is Ui::MainWindow. The ui in MainWindow is initialized with the layout class. After that, you need to use ui->setupUI(this); in the constructor to apply the layout in Ui::MainWindow to the local MainWindow.
1.4 mainwindow.ui layout file
The content is a markup file in xml format, which is consistent with the mainstream UI design. Now the mainstream UI design such as Andriod and WP use XML files for UI design. You can use the Qt Designer that comes with the Qt SDK for visual design. The designed *.ui file can be used directly in the project file. In the main .pro, add the corresponding file name to FORMS, and add the #include "ui_*.h" file name to the *.cpp file. After initialization, you can build the layout in your own class.
2. Be sure to understand ui_mainwindow.h
The header file of this UI is generated after compiling the original project, which will involve the function for real layout, that is, the MainWindow in the Ui domain. See the code below:
#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H
 
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHeaderView>
#include <QtGui/QMainWindow>
#include <QtGui/QMenuBar>
#include <QtGui/QStatusBar>
#include <QtGui/QToolBar>
#include <QtGui/QWidget>
 
QT_BEGIN_NAMESPACE
 
class Ui_MainWindow
{
public:
QMenuBar *menuBar;
QToolBar *mainToolBar;
QWidget *centralWidget;
QStatusBar *statusBar;
 
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(600, 400);
menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
MainWindow->setMenuBar(menuBar);
mainToolBar = new QToolBar(MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MainWindow->addToolBar(mainToolBar);
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
MainWindow->setCentralWidget(centralWidget);
statusBar = new QStatusBar(MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MainWindow->setStatusBar(statusBar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
 
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0, QApplication::UnicodeUTF8));
Q_UNUSED(MainWindow);
} // retranslateUi
};
 
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
 
QT_END_NAMESPACE
#endif // UI_MAINWINDOW_H
Whoosh, it's a lot more at once, but it's actually quite easy. Ui_MainWindow declares several components, I won't say it specifically, because there is nothing to say, it implements the setupUi function, which is the setupUi called in the previous MainWindow.

But it should be noted that the QMetaObject::connectSlotsByName() function will automatically connect the signal and slot of the corresponding name, but it should be noted that it is connected to the incoming MainWindow and its subcomponents [not subclasses], pay attention to the front ui->setupUi( This passed in this) is the MainWindow in the non-ui domain, so if you want to declare the signal and slot in the MainWindow in the non-ui domain, you should declare it in the MainWindow of the non-ui domain, and then interact with the GUI in the form of ui->xxx ! This is easily demonstrated if we drag and drop a button in QtDesiner and click go to slot.

retranslateUi will name the components in the ui, and I will not say more here.

What we need to focus on is the following code. And think, after compiling Designer, it is understandable to generate a layout, why is there an additional namespace?
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
The *ui ( UI::MainWindow *ui ) of MainWindow in the non-Ui domain above points to the MainWindow in the Ui domain, and the MainWindow (the line of code above) in the Ui domain inherits Ui_MainWindow
3. What exactly is UI?
ui is usually the suffix of interface files designed by Qt designers. Usually ui is a pointer to this interface class. "ui->" is generally used to access the controls in this interface class.
For example, we have a QPushButton called OkButton in our ui file. We can access the button ui->OkButton like this. setupUi(this) is the constructor of the class generated by the .ui file. The function of this function is to initialize the interface.
It draws the form as we designed it in the Qt designer, and builds up the signals and slots we defined in the Qt designer. It can also be said that setupUi is the bridge between us drawing the interface and writing the program.
———————————————
Copyright statement: This article is an original article by CSDN blogger "Shen Ziheng", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/shenziheng1/article/details/60765502

CONTACT US

Contact: Manager Xu

Phone: 13907330718

Tel: 0731-22222718

Email: hniatcom@163.com

Add: Room 603, 6th Floor, Shifting Room, No. 2, Orbit Zhigu, No. 79 Liancheng Road, Shifeng District, Zhuzhou City, Hunan Province

Scan the qr codeClose
the qr code