1. 程式人生 > 其它 >QT的第一個入門小程式

QT的第一個入門小程式

  1. 專案的檔案結構

  2. 工程類檔案 0708day08.pro

#-------------------------------------------------
#
# Project created by QtCreator 2021-07-08T10:11:34
#
#-------------------------------------------------
#QT包含的模組,目前來看主要包含了兩個模組
QT       += core gui
#要判定QT的版本,如果版本大於4 則加入這個模組
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#編譯生成的exe檔案的名稱,在編譯檔案中可以看到
#我的檔案路徑為:E:\QtProjects\build-0708day08-#Desktop_Qt_5_9_0_MinGW_32bit-Debug\debug\XXX.exe
TARGET = 0708day08
#應用程式模板
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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 \
        mainwindow.cpp
#標頭檔案
HEADERS += \
        mainwindow.h
#介面檔案
FORMS += \
        mainwindow.ui

  1. 標頭檔案
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    //巨集,允許類中使用訊號和槽
    Q_OBJECT

public:
    //建構函式
    explicit MainWindow(QWidget *parent = 0);
    //解構函式
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
  1. 原始檔
#include "mainwindow.h"
#include "ui_mainwindow.h"
//程式設計小Tips
//類名 首字母大寫 單詞之間首字母大寫
//函式名 變數名 首字母小寫
//快捷鍵
// 註釋 ctrl + / 
// 執行 ctrl + r 
// 編譯 ctrl + b 
// 對齊 ctrl + i
// f1幫助介面  esc 退出  或者在目錄D:\QT\Qt5.9\5.9\mingw53_32\bin 使用幫助文件
//自動對齊  ctrl + i
//.h(標頭檔案) 和.cpp(原始檔)文之間的切換使用f4按鈕
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

  1. 建立一個按鈕
  • 在幫助文件中查詢相關資訊

  • 閱讀幫助文件


得知:需要引入的標頭檔案的名稱,需要的模組,父類及其子類關係

  • 建立按鈕(方式一)
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //建立一個按鈕
    QPushButton * btn = new QPushButton;
    //show以頂層方式彈出,需要讓btn依賴mywidget
    btn->setParent(this);
    //顯示文字
    btn->setText("第一個按鈕");
    btn->resize(100,50);
}

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

這裡的三行程式碼
首先需要建立按鈕,之後需要讓按鈕顯示出來,使用show方法會讓按鈕以頂層視窗的方式彈出,這裡採用設定按鈕的依賴物件,然後設定按鈕的顯示文字,即可執行

  • 建立按鈕(方式二)
//三句話,合併成一句話
QPushButton * btn = new QPushButton("第二個按鈕",this);
//補充
    //設定固定視窗大小
    setFixedSize(600,400);
    //設定視窗標題
    setWindowTitle("第一個視窗");
  • 建立的按鈕中文可能出現亂碼的情況


    修改之後,儲存即可。
  1. 物件樹
    在Qt中建立物件的時候會提供一個Parent物件指標,在建立QObject物件時,可以提供一個其父物件,我們建立的這個QObject物件會自動新增到其父物件的children()列表。當父物件析構的時候,這個列表中的所有物件也會被析構。(注意,這裡的父物件並不是繼承意義上的父類!)任何物件樹中的 QObject物件 delete 的時候,如果這個物件有 parent,則自動將其從 parent 的children()列表中刪除;如果有孩子,則自動 delete 每一個孩子。
  • 建立自定義按鈕類
    通過在專案上右擊,新增新檔案,選擇新增c++class,設定標頭檔案和原始檔 自動生成
  • 標頭檔案編寫構造和解構函式宣告,原始檔編寫構造和解構函式具體細節

mypushbutton.h 標頭檔案

#ifndef MYPUSHBUTTON_H
#define MYPUSHBUTTON_H
#include <QPushButton>
#include <QWidget>

class MyPushButton : public QPushButton
{
    Q_OBJECT
public:
    explicit MyPushButton(QWidget *parent = nullptr);
    ~MyPushButton();
    signals:

public slots:

};

#endif // MYPUSHBUTTON_H

mypushbutton.cpp 原始檔

mypushbutton.cpp

mypushbutton.cpp 原始檔

#include "mypushbutton.h"
#include <QPushButton>
#include <QDebug>
MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
{
    qDebug()<<"我的按鈕類的構造呼叫";
}

MyPushButton::~MyPushButton(){
    qDebug()<<"我的按鈕類的析構呼叫";
}


  • 加入至物件樹
    在mainwindow.cpp中引入自定義的按鈕類,相當於加入到children中
      MyPushButton * mbtn = new MyPushButton();
      mbtn->setParent(this);
      mbtn->setText("我自己的按鈕");
      mbtn->resize(50,30);
  • 測試執行效果

    這個看起來好像是mainwindow先析構,然後才是自己建立的按鈕類的析構。事實上,只是先執行了qdebug的那行話,然後查詢mainwindow下的所有子物件,分別進入,進入後依舊先執行列印一句話,判別我的按鈕類下面還有沒有子物件,如果沒有的話就執行析構,然後再返回上一層。這個類似於樹的遞迴遍歷!