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

widget.h

#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
{
    Q_OBJECT
public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private:
    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.
};

widget.cpp

#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.
{
    ui->setupUi(this);
    //setupUi realizes the generation of windows, the setting of various attributes, and the correlation between signals and slots
}
Widget::~Widget()
{
    delete ui;
}

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>336</width>
    <height>216</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>My First Demo</string>
  </property>
  <widget class="QLabel" name="Lablabel">
   <property name="geometry">
    <rect>
     <x>100</x>
     <y>70</y>
     <width>141</width>
     <height>61</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <pointsize>12</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>Hello,World</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btnClose">
   <property name="geometry">
    <rect>
     <x>210</x>
     <y>150</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>Close</string>
   </property>
  </widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections>
  <connection>
   <sender>btnClose</sender>
   <signal>clicked()</signal>
   <receiver>Widget</receiver>
   <slot>close()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>247</x>
     <y>161</y>
    </hint>
    <hint type="destinationlabel">
     <x>167</x>
     <y>107</y>
    </hint>
   </hints>
  </connection>
</connections>
</ui>

ui_widget.h
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>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
    QLabel *label;
    QPushButton *btnClose;
    void setupUi(QWidget *Widget)
{
        if (Widget->objectName().isEmpty())
            Widget->setObjectName(QStringLiteral("Widget"));
        Widget->resize(336, 216);
        label = new QLabel(Widget);
        label->setObjectName(QStringLiteral("label"));
        label->setGeometry(QRect(100, 70, 141, 61));
        QFont font;
        font.setPointSize(12);
        font.setBold(true);
        font.setWeight(75);
        label->setFont(font);
        btnClose = new QPushButton(Widget);
        btnClose->setObjectName(QStringLiteral("btnClose"));
        btnClose->setGeometry(QRect(210, 150, 75, 23));
        retranslateUi(Widget);
        QObject::connect(btnClose, SIGNAL(clicked()), Widget, SLOT(close()));
        QMetaObject::connectSlotsByName(Widget);
    } // 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
QT_END_NAMESPACE
#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()));

QMetaObject::connectSlotsByName(Widget);
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.