Analysis of file dependence of Qt in creating Application

When using QtCreator to create Application program, it will automatically create * * *. UI file in the folder "buil...

When using QtCreator to create Application program, it will automatically create * * *. UI file in the folder "build - * * * - Desktop"_ Qt_ ***-Generate UI in debug_ ***. h, * * *. UI can be adjusted through QtDesigner, and a c + + class with the same name will be created in the folder of * * *. UI. This brings us a lot of doubts. How does QT generate UI? Here we do a simple analysis.

mainwindow.ui File:

<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget name="centralwidget"/> <widget name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>23</height> </rect> </property> </widget> <widget name="statusbar"/> </widget> <resources/> <connections/> </ui>

Auto generated ui_mainwindow.h file:

/******************************************************************************** ** Form generated from reading UI file 'mainwindow.ui' ** ** Created by: Qt User Interface Compiler version 5.12.8 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef UI_MAINWINDOW_H #define UI_MAINWINDOW_H #include <QtCore/QVariant> #include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <QtWidgets/QMenuBar> #include <QtWidgets/QStatusBar> #include <QtWidgets/QWidget> QT_BEGIN_NAMESPACE class Ui_MainWindow { public: QWidget *centralwidget; QMenuBar *menubar; QStatusBar *statusbar; void setupUi(QMainWindow *MainWindow) { if (MainWindow->objectName().isEmpty()) MainWindow->setObjectName(QString::fromUtf8("MainWindow")); MainWindow->resize(800, 600); centralwidget = new QWidget(MainWindow); centralwidget->setObjectName(QString::fromUtf8("centralwidget")); MainWindow->setCentralWidget(centralwidget); menubar = new QMenuBar(MainWindow); menubar->setObjectName(QString::fromUtf8("menubar")); menubar->setGeometry(QRect(0, 0, 800, 23)); MainWindow->setMenuBar(menubar); 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", nullptr)); } // retranslateUi }; namespace Ui { class MainWindow: public Ui_MainWindow {}; } // namespace Ui QT_END_NAMESPACE #endif // UI_MAINWINDOW_H

Here, we can see that there is UI in the file_ MainWindow class, but this class does not inherit from Qt class, so the displayed form is not Ui_MainWindow, but it contains the UI controls, and there is a void setupUi(QMainWindow *MainWindow) method. The parameter passed in by this method is a Qt UI class, and the UI class passed in is UI_ Controls in MainWindow. Finally, define inheritance UI in namespace Ui_MainWindow class of MainWindow.

In the automatically generated c + + class, the h/cpp file is as follows:

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QAction> 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; // QAction is added by myself QAction* new_action_; QAction* exit_action_; }; #endif // MAINWINDOW_H
#include <QAction> #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); new_action_ = new QAction(tr("&New"), this); new_action_->setShortcut(QKeySequence::New); exit_action_ = new QAction(tr("E&xit"), this); exit_action_->setShortcut(QKeySequence::Quit); menuBar()->addAction(new_action_); } MainWindow::~MainWindow() { delete ui; }

We can see that MainWindow inherits QMainWindow, and MainWindow has a private variable Ui::MainWindow *ui (the Ui space mentioned above inherits Ui_MainWindow class of MainWindow), in the constructor Ui - > setupui (this).

Conclusion:

  1. The UI displayed after the program is actually the MainWindow in the c++ file.
  2. The controls in MainWindow use UI - > setupui (this) to set UI_ In the MainWindow class, assign MainWindow.
  3. Ui_MainWindow and UI files are actually just control libraries and layout frameworks, and do not really display the UI.

31 May 2020, 00:41 | Views: 7229

Add new comment

For adding a comment, please log in
or create account

0 comments