Qt notes (15) definition of form class & implementation principle of interface function

Posted by soulroll on Sat, 12 Feb 2022 04:02:08 +0100

Reprint this link

file namefunction
widget.hDefines the header file of the form class, defines the class Widget, and inherits from QWidget
widget.cppThe function implementation source program file of Widget class
widget.uiThe form interface file is automatically generated by the UI Designer and stores the property settings and layout of each component on the form
ui_widget.hAfter compilation, a class definition file is automatically generated according to the components on the form and their properties, the association between signals and slots, etc. the name of the class is Ui_Widget


#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
//This class Widget is not the class Widget defined in this file, but the UI_ Widget. The class defined in H file is used to describe the of interface components. This declaration is equivalent to an external type declaration
class Widget : public QWidget
    explicit Widget(QWidget *parent = 0);
    Ui::Widget *ui;
    //This pointer is defined by the Widget class in namespace Ui declared earlier, so the pointer ui points to the visual design interface. Later, you will see that to access the components on the interface, you need to use this pointer ui.


#include "widget.h"
#include "ui_widget.h" 
//The UI file generated by Qt compilation is the same as the widget Class definition file corresponding to UI
Widget::Widget(QWidget *parent) :QWidget(parent),
    ui(new Ui::Widget)
    //Its meaning is to execute the constructor of the parent QWidget and create an ui object of Ui::Widget class. This ui is the pointer variable ui defined in the private part of the Widget.
    //setupUi realizes the generation of windows, the setting of various attributes, and the correlation between signals and slots
    delete ui;


<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<widget class="QWidget" name="Widget">
  <property name="geometry">
  <property name="windowTitle">
   <string>My First Demo</string>
  <widget class="QLabel" name="Lablabel">
   <property name="geometry">
   <property name="font">
   <property name="text">
  <widget class="QPushButton" name="btnClose">
   <property name="geometry">
   <property name="text">
<layoutdefault spacing="6" margin="11"/>
    <hint type="sourcelabel">
    <hint type="destinationlabel">

ui_widget.h is on the widget A file generated after compiling the UI file

** Form generated from reading UI file 'widget.ui'
** Created by: Qt User Interface Compiler version 5.9.1
** WARNING! All changes made in this file will be lost when recompiling UI file!
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
class Ui_Widget
    QLabel *label;
    QPushButton *btnClose;
    void setupUi(QWidget *Widget)
        if (Widget->objectName().isEmpty())
        Widget->resize(336, 216);
        label = new QLabel(Widget);
        label->setGeometry(QRect(100, 70, 141, 61));
        QFont font;
        btnClose = new QPushButton(Widget);
        btnClose->setGeometry(QRect(210, 150, 75, 23));
        QObject::connect(btnClose, SIGNAL(clicked()), Widget, SLOT(close()));
    } // setupUi
    void retranslateUi(QWidget *Widget)
        Widget->setWindowTitle(QApplication::translate("Widget", "My First Demo", Q_NULLPTR));
        label->setText(QApplication::translate("Widget", "Hello\357\274\214World", Q_NULLPTR));
        btnClose->setText(QApplication::translate("Widget", "Close", Q_NULLPTR));
    } // retranslateUi
namespace Ui {
    class Widget: public Ui_Widget {};
} // namespace Ui
#endif // UI_WIDGET_H

ui_widget.h document mainly does the following work
1. A class UI is defined_ Widget, used to encapsulate the interface of visual design.
2. Automatically generate the class member variable definitions of each component of the interface.
3. The setupUi() function is defined. This function is used to create each interface component, set its location, size, text content, font and other attributes, and set the association between the signal and the slot.
setupUi() calls the function retranslateUi(Widget), which is used to set the text content properties of each component of the interface, such as the text of the label, the text of the key, the title of the form, etc. Separate the text settings on the interface as a function retranslateUi(), which will be used when designing multilingual interfaces.
The third part of the setupUi() function is to set the correlation between the signal and the slot
QObject::connect(btnClose, SIGNAL(clicked()), Widget, SLOT(close()));

The second line is to set the correlation method of slot function, which is used to associate the slot function of component signal automatically generated by UI designer with component signal.
4. Define namespace Ui and a slave Ui_Widget class inherited by widget.

namespace Ui {
    class Widget: public Ui_Widget {};

Ui_Widget. The class that implements the interface function in the H file is Ui_Widget. Then define a class widget from Ui_Widget is inherited and defined in namespace Ui, so Ui:: Widget and widget The class widget in H has the same name, but it is distinguished by namespace

Therefore, the Ui:: Widget class of the interface is the same as the file Widget The Widget class defined in H is actually two classes, but the processing of Qt makes users feel that Ui:: Widget class does not exist. They only need to know that the ui pointer in Widget class can access the interface components of visual design.