Basic knowledge of Qt source code

 untitled1.pro

#-------------------------------------------------
#
# Project created by QtCreator 2022-06-28T09:57:30
#
#-------------------------------------------------
# Qt应用到的模块
QT       += core gui
# 兼容以前的Qt版本(一般不会更改)
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# 生成应用程序的名字
TARGET = untitled1
# 指生成的makfile类型
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
# UI文件
FORMS += \
        widget.ui
# The module that Qt applies to
QT       += core gui
# Compatible with previous Qt versions (generally will not change)
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# Generate the name of the application
TARGET = untitled1
# Refers to the generated makfile type
TEMPLATE = app

widget.h 

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
//定义UI的命名空间
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT    //宏 如果使用信号和槽,必须添加此类

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui; //用UI命名空间里面的Widget类,定义一个指针*ui
};

#endif // WIDGET_H

Q_OBJECT //If the macro uses signals and slots, you must add this class

Ui::Widget *ui; //Use the Widget class in the UI namespace to define a pointer *ui

 widget.cpp

#include "widget.h"
#include "ui_widget.h"

//构造函数
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

 main.cpp

#include "widget.h"     //因为主函数用了窗口,需要导入头文件
#include <QApplication>

int main(int argc, char *argv[])
{
    //应用程序类,每个Qt应用程序中都有且只有一个应用程序对象
    QApplication a(argc, argv);
    //窗口类创建窗口对象,窗口创建出来默认不显示
    Widget w;   //顶层窗口
    w.show();   //窗口显示

    return a.exec();    //应用程序对象运行
}

What is the function of exec()?

The function is to open a loop and execute an event, which is equivalent to while(1) and for(;;). Ensure that the window is not flashed

Guess you like

Origin blog.csdn.net/m0_52177571/article/details/125498893