• 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
  • 相关阅读:
    STL笔记
    精选PHP毕业设计12套——源码+论文完整资源
    js 中的 map集合使用。
    2022-08-30 第六小组 瞒春 学习笔记
    Python——基础知识
    9.WPF资源
    从零开发一款相机APP 第十篇:Camera2 zoom变焦
    C++语言的return语句的说明
    【Markdown】图片缩放
    欧奈尔RPS曲线的编制方法及常见问题解答
  • 原文地址:https://blog.csdn.net/bdn_nbd/article/details/133856833