• QT学习笔记1-Hello, QT


    1. QT环境

    1.1 QT_CREATOR

    QT的集成开发工具,可以进行项目的创建运行。有一些实例可以运行之。

    1.2 QT_ASSISTANT

    QT的工具书

    2. 核心的概念

    2.1 windows

    窗口

    2.2 widget

    组件放置在窗口上的

    2.3 bar

    2.4 icon

    图标

    3. Hello, QT

    3.1 main.cpp
    #include "mainwindow.h"
    
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        MainWindow w;
    
        w.show();
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    返回值与普通的C++程序不同的地方是,返回的是QApplication.exec(), 这是一个阻塞的循环,一直接受消息。

    3.2 mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include 
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
        
    private:
        Ui::MainWindow *ui;
    };
    #endif // MAINWINDOW_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    Q_OBJECT是一个宏定义,当使用信号槽时,则需要加上,而且是在private属性下加(CPP默认成员属性为私有)。

    3.3 mainwindow.cpp

    主窗口的构造析构函数实现

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    3.4 HelloQt.pro

    qt是一个CPP的图形界面框架,当然可以用cmake; 但也提供了自己的工具qmake

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++17
    
    # You can make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    SOURCES += \
        main.cpp \
        mainwindow.cpp
    
    HEADERS += \
        mainwindow.h
    
    FORMS += \
        mainwindow.ui
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    Unity3D学习笔记12——渲染纹理
    PMI-ACP练习题(30)
    在.Net 5或更高版本的.Net中使用appsettings.json配置文件
    TiUP 镜像参考指南
    SecureCRT SSH与FTP连接中文乱码
    LVS负载均衡集群
    datagridview设置随窗口改变大小
    【从零开始学微服务】01.微服务的过去与现在
    不懂23种设计模式?别灰心,这份核心笔记来帮你,你想知道的都在这里!
    二维码怎么做列表?点击可跳转其他内容
  • 原文地址:https://blog.csdn.net/bdn_nbd/article/details/133856833