1 GUI程序结构与运行机制
1.1 项目配置文件 【.pro】
后缀“.pro”文件为项目配置文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
QT += widgets + 空格 + 模块名(库名)
作用:用来配置Qt系统的库,也包含头文件路径
加载Qt 内部库 (例如:QT += core gui,去掉对某个库的加载:QT -= gui)
#在项目中使用Qt SQL模块
QT += sql
1.2 文件widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; } //Widget为文件ui_widget.h里面定义的一个类用于描述可视化设计
//的窗口界面,该命名空间包含一个类widget
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT //插入这个宏之后,widget类中就可以使用信号与槽、属性等功能
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
//ui是窗口UI类的对象指针指向可视化窗口界面要访问窗口上面的组件,就通过该指针来实现
//即ui相当于一个父类指针,通过父类指针可以访问多个不同的子类对象
};
#endif // WIDGET_H
1.3 文件widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent):QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this); //子类通过指针来访问父类中的函数
//this是Widget实例对象
}
Widget::~Widget()
{
delete ui;
}
ui->setupUi(this); 表示运行了Ui::Widget类的setupUi()函数,并且以this作为函数setupUi()的输入参数,this就是Widget类对象的实例,也就是一个窗口。setupUi()函数里会创建窗口上所有的界面组件,并且以Widget窗口作为所有组件的父容器;
1.4 文件ui_widget.h
/********************************************************************************
** Form generated from reading UI file 'widget.ui'
**
** Created by: Qt User Interface Compiler version 5.14.2
**
** 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/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QLabel *label;
QPushButton *pushButton;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(355, 220);
label = new QLabel(Widget);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(110, 70, 131, 41));
QFont font;
font.setFamily(QString::fromUtf8("Times New Roman"));
font.setPointSize(20);
font.setBold(true);
font.setWeight(75);
label->setFont(font);
pushButton = new QPushButton(Widget);
pushButton->setObjectName(QString::fromUtf8("pushButton"));
pushButton->setGeometry(QRect(260, 180, 81, 31));
QFont font1;
font1.setFamily(QString::fromUtf8("Times New Roman"));
font1.setPointSize(18);
font1.setBold(true);
font1.setWeight(75);
pushButton->setFont(font1);
retranslateUi(Widget); #设置界面上各组件的文字属性
//设置信号与槽的关联
QObject::connect(pushButton, SIGNAL(clicked()), Widget, SLOT(close()));
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
label->setText(QCoreApplication::translate("Widget", "Hello Qt6!", nullptr));
pushButton->setText(QCoreApplication::translate("Widget", "close", nullptr));
} // retranslateUi
};
//定义名字空间Ui,并定义一个类Widget从Ui_Widget继承而来;
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
注意:
Ui_Widget类无父类,不是从QWidget继承而来,故该类不是一个窗口类