Qt5教程(一):Hello World 程式

1。 新建工程

Welcome

介面選擇

New Project

——>

Application

——>

Qt Widgets Application

——>

Choose

Qt5教程(一):Hello World 程式

輸入工程名稱Name, 並選擇工程存放路徑, 點選

Next

。 下一個頁面繼續點選

Next

(是否作為預設工程路徑, 自己決定勾不勾選)。

Qt5教程(一):Hello World 程式

選擇

Base class

Qwidget

, 輸入

Class name

(自定義), 取消勾選

Generate form

(不新增ui檔案), 點選

Next

, 再點選

Finish

, 工程就建立好了。

Qt5教程(一):Hello World 程式

QMainWindow 為主視窗類, 帶主選單欄、工具欄和狀態列

QWidget 為所有可視介面的基類, QWidget建立的介面支援所有介面元件

QDialog 為對話方塊

2。 專案檔案介紹

2.1 main.cpp

#include “mywidget。h”/* * QApplication為應用程式類 * Qt的標頭檔案沒有。h, 前2個字母都大寫 * Qt類名和標頭檔案名一樣 */#include int main(int argc, char *argv[]){ // 一個應用程式有且只有一個應用程式類的物件 // 這裡的a即應用程式類物件 QApplication a(argc, argv); // myWidget是建立工程時建立的, 繼承於QWidge // QWidget是一個視窗基類 myWidget w; // 視窗建立預設影藏的, 需要人為顯示 w。show(); // 相當於: // a。exec(); // return 0; // exec()功能是讓程式一直執行, 等待事件的發生 return a。exec();}

main檔案的整個框架如下:

#include

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

// 程式碼

return a。exec();

}

2.2 mywidget.h

#ifndef MYWIDGET_H#define MYWIDGET_H#include // 建立一個類myWidget, 繼承於QWidgetclass myWidget : public QWidget{ Q_OBJECT // 訊號與槽時需要, 用不到可註釋掉public: myWidget(QWidget *parent = 0); // 建構函式: 建立物件時自動執行 ~myWidget(); // 屬性函式};#endif // MYWIDGET_H

2。 3 HelloWorld。pro

#————————————————————————-## Project created by QtCreator 2019-08-06T15:06:28##————————————————————————-# 用#註釋一行# 新增所需模組QT += core gui # 當前版本高於Qt4, 則加入widgets模組greaterThan(QT_MAJOR_VERSION, 4): QT += widgets# HelloWorld為生成的可執行檔名TARGET = HelloWorld# 制定makefile的型別為應用程式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。0CONFIG += c++11# 專案中包含的源程式檔案SOURCES += \ main。cpp \ mywidget。cpp# 專案中包含的標頭檔案HEADERS += \ mywidget。h# Default rules for deployment。qnx: target。path = /tmp/$${TARGET}/binelse: unix:!android: target。path = /opt/$${TARGET}/bin!isEmpty(target。path): INSTALLS += target

3。 執行程式

使用 Ctrl + R 快捷鍵, 或點選左下角的 Run 圖示:

Qt5教程(一):Hello World 程式

執行後會顯示一個空白視窗, 視窗名為HelloWorld。

Qt5教程(一):Hello World 程式

4。 第一個Hello World程式

(完整的程式在本文最後會提供)

4。1 新建空白視窗

現在我們再新建一個空白專案, 從零手寫一個Hello World程式。

點選

File

——>

New File or Project

, 或使用快捷鍵 Ctrl + N, 這次我們選擇

Other Project

——>

Empty qmake

——>

Choose

Qt5教程(一):Hello World 程式

然後輸入專案名稱, 選擇工程存放路徑, 之後一路Next, Finish。

Qt5教程(一):Hello World 程式

新生成的專案只有一個空的Hello。pro檔案

Qt5教程(一):Hello World 程式

首先, 在Hello。pro中新增圖形介面模組, 輸入:

Qt += widgets

之後, 新增一個main。cpp檔案, 右擊Hello資料夾 ——>Add New(新增新檔案) ——> C++ ——> C++ Source File ——> Choose

Qt5教程(一):Hello World 程式

檔名Name為: main。cpp, 存放路徑Path預設即可, 然後點選Next。

Qt5教程(一):Hello World 程式

之後點選Finish即可

Qt5教程(一):Hello World 程式

main。cpp就被新增到工程中了。

Qt5教程(一):Hello World 程式

在mian。cpp檔案中先新增上面講的框架:

#include int main(int argc, char *argv[]){ QApplication a(argc, argv); return a。exec();}

之後, 我們需要建立一個視窗,

#include #include // 新增視窗控制元件的標頭檔案int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget w; // 建立一個視窗物件 w。show(); // 顯示視窗 return a。exec();}

現在Ctrl + R 執行, 就會出現一個空白視窗了。

Qt5教程(一):Hello World 程式

4。2 需改視窗標題

透過

setWindowTitle()

來設定視窗標題:

Qt5教程(一):Hello World 程式

4。3 新增按鈕

和建立視窗一樣的步驟, 先新增標頭檔案, 再建立物件, 設定屬性:

#include #include #include int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget w; w。setWindowTitle(“Hahaha”); w。show(); QPushButton b; // 建立按鈕物件 b。setText(“Click”); // 設定按鈕內容 return a。exec();}

執行後發現並沒有按鈕, 所以應該也是要人為顯示的, 新增

b。show(); // 顯示按鈕b

再次執行, 按鈕出現了:

Qt5教程(一):Hello World 程式

可是按鈕並不是在我們那個視窗上顯示的, 而是按鈕自成一個視窗, 這並不是我們想要的效果。

在Qt中,

物件和物件是獨立的

(我們這裡的視窗和按鈕是獨立的), 但

如果給一個物件指定父物件, 那麼該物件就會在父物件上面顯示

(如果指定視窗為按鈕的父物件, 則按鈕會在視窗上顯示), 而

指定父物件的方法有兩種: (1) setParent; (2) 透過建構函式傳參.

如果指定了父物件, 則父物件顯示後, 子物件會自動顯示。

下面我們重寫按鈕的程式:

#include

#include

#include

int main(int argc, char *argv[])

{

QApplication a(argc, argv);

QWidget w;

w。setWindowTitle(“Hello World”);

QPushButton b;

b。setText(“Click”);

b。setParent(&w);

w。show();

return a。exec();

}

按鈕就出現在我們的視窗上了:

Qt5教程(一):Hello World 程式

透過

move()

設定按鈕位置:

b。move(100, 100);

Qt5教程(一):Hello World 程式

附錄

main。cpp

#include #include #include int main(int argc, char *argv[]){ QApplication a(argc, argv); QWidget w; w。setWindowTitle(“Hello World”); QPushButton b; b。setText(“Click”); b。setParent(&w); b。move(100, 100); w。show(); return a。exec();}

Hello。pro

Qt += widgets

# 下面兩行是自動新增的

SOURCES += \

main。cpp