1. What is a UI?
The UI is usually the suffix of the interface file designed by the QT designer.
Typically, the UI is a pointer to the interface class.
Ui-> is generally used to access the controls inside the interface class.
For example, there is a qpushbutton called OKButton in your UI file.
You can then access this button Ui->okbutton.
SETUPUI (this) is the constructor of the class generated by the. ui file, which is the function of initializing the interface,
It draws the form according to what we have designed in the QT designer and sets up the signals and slots we define in the QT designer.
It can also be said that Setupui is the bridge between our drawing interface and writing programs.
2. Using Qtcreator to select GUI applications will produce projects containing the following VSQT files
The 3.pro file is a file for qmake use.
4.main.cpp
Code:
#include "Mainwindow.h"
#include "Ui_mainwindow.h"
Mainwindow::mainwindow (Qwidget *parent):
Qmainwindow (parent),
UI (New Ui::mainwindow)
{
UI->SETUPUI (this);
}
Mainwindow::~mainwindow ()
{
Delete UI;
}
Introduced:
Qapplication A (argc, argv) and a.exec () can be understood as a schema loaded into QT.
It calls a MainWindow and show it out.
5.mainwindow.h
Code:
#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_h
Introduced:
The beginning of the namespace UI may be a bit confusing because QT makes the UI-related classes stand alone.
Declaring the namespace UI is because you want to invoke MainWindow in the UI, this MainWindow is not MainWindow, and the *ui pointer involved will call it!
The classes related to signal and slots in Q_OBJECT,QT are declared as such.
*ui will generate a form.
6.mainwindow.cpp
Code:
#include "Mainwindow.h"
#include "Ui_mainwindow.h"
Mainwindow::mainwindow (Qwidget *parent):
Qmainwindow (parent),
UI (New Ui::mainwindow)
{
UI->SETUPUI (this);
}
Mainwindow::~mainwindow ()
{
Delete UI;
}
Introduced:
Constructs a MainWindow in a UI domain on the heap, and calls Setupui, and the destructor is simply delete!
As mentioned earlier, QT separates the UI very well, and the. ui file in the previous diagram is the layout file that makes Qtdesigner!
7.ui_mainwindow.h
/********************************************************************************
* * Form generated from reading UI file ' Mainwindow.ui '
**
* * Created by:qt User Interface Compiler version 5.4.0
**
* * warning! All changes made in this file would be lost when recompiling UI file!
********************************************************************************/
#ifndef Ui_mainwindow_h
#define Ui_mainwindow_h
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>
Qt_begin_namespace
Class Ui_mainwindow
{
Public
Qwidget *centralwidget;
Qpushbutton *pushbutton;
Qlabel *label;
Qmenubar *menubar;
Qtoolbar *maintoolbar;
Qstatusbar *statusbar;
void Setupui (Qmainwindow *mainwindow)
{
if (Mainwindow->objectname (). IsEmpty ())
Mainwindow->setobjectname (Qstringliteral ("MainWindow"));
Mainwindow->resize (400, 300);
Centralwidget = new Qwidget (MainWindow);
Centralwidget->setobjectname (Qstringliteral ("Centralwidget"));
pushbutton = new Qpushbutton (centralwidget);
Pushbutton->setobjectname (Qstringliteral ("pushbutton"));
Pushbutton->setgeometry (Qrect (170, 180, 75, 23));
label = new Qlabel (centralwidget);
Label->setobjectname (qstringliteral ("label"));
Label->setgeometry (Qrect (50, 70, 261, 16));
Mainwindow->setcentralwidget (Centralwidget);
MenuBar = new Qmenubar (MainWindow);
Menubar->setobjectname (Qstringliteral ("MenuBar"));
Menubar->setgeometry (qrect (0, 0, 400, 23));
Mainwindow->setmenubar (MenuBar);
Maintoolbar = new Qtoolbar (MainWindow);
Maintoolbar->setobjectname (Qstringliteral ("Maintoolbar"));
Mainwindow->addtoolbar (Qt::toptoolbararea, Maintoolbar);
StatusBar = new Qstatusbar (MainWindow);
Statusbar->setobjectname (Qstringliteral ("StatusBar"));
Mainwindow->setstatusbar (StatusBar);
Retranslateui (MainWindow);
Qmetaobject::connectslotsbyname (MainWindow);
}//Setupui
void Retranslateui (Qmainwindow *mainwindow)
{
Mainwindow->setwindowtitle (Qapplication::translate ("MainWindow", "MainWindow", 0));
Pushbutton->settext (Qapplication::translate ("MainWindow", "OK", 0));
Label->settext (Qapplication::translate ("MainWindow", "This was a qt and vs Projec to create exe.", 0));
}//Retranslateui
};
Namespace Ui {
Class Mainwindow:public Ui_mainwindow {};
}//Namespace Ui
Qt_end_namespace
#endif//Ui_mainwindow_h
Introduced:
Ui_mainwindow declares several artifacts that implement the SETUPUI function, which is the SETUPUI called in the previous MainWindow.
However, it is important to note that the Qmetaobject::connectslotsbyname function automatically connects the signal and slot of the corresponding name, but notice that it is connected to the incoming MainWindow and its sub-members "not subclasses", notice that the front ui->setupui ( This is the MainWindow in the non-UI domain, so if you want to declare signal and slots, declare them in the MainWindow of the non-UI domain, and then interact with the GUI in the form of ui->xxx!
Retranslateui will name the artifacts in the UI.
The *ui of MainWindow in the front non-UI domain points to MainWindow in the UI domain, and MainWindow in the UI domain inherits Ui_mainwindow.
Introduction to the use of the UI in Qt