粉絲不過W
下載地址:https://www.qt.io/download-open-source
打開 Qt Creator 界面選擇 New Project 或者選擇菜單欄 【文件】 -【新建文件或項目】 菜單項
彈出 New Project 對話框,選擇 Application,選擇 Qt Widgets Application ,選擇 Choose 按鈕, 彈出如下對話框
設置項目名稱和路徑,按照嚮導進行下一步,
選擇編譯套件
嚮導會默認添加一個繼承自 CMainWindow 的類,可以在此修改類的名字和基類。 默認的基類有 QMainWindow、 QWidget 以及 QDialog 三個,我們可以選擇 QWidget(類似於空窗口)
系統會默認給我們添加 文件 和一個.pro項目文件,點擊完成, 即可創建出一個 Qt 桌面程序。
點擊運行,馬上就要看見成功了
爲綠色就成功了,這也是成功男士看破紅塵的時候,既然綠了自然就有結果了
這就是成功的結果,現在就是看看結果的準確性了(做一下親子鑑定)
分析一下代碼:
myqt.pro
工程文件(project),它是 qmake 自動生成的用於生產 makefile 的配置文件
#-------------------------------------------------
#
# Project created by QtCreator 2020-06-18T17:23:42
#
#-------------------------------------------------
# 包含的模塊
QT += core gui
# 大於 Qt4 版本才包含 widget 模塊
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
# 應用程序名 生成的.exe 程序名稱
TARGET = myqt
# 模板類型 應用程序模板
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 \
widget.cpp
# 頭文件
HEADERS += \
widget.h
# .ui 設計文件
FORMS += \
widget.ui
main.cpp
#include "widget.h"
//標準類名聲明頭文件沒有.h 後綴
#include <QApplication>
int main(int argc, char *argv[])
{
// 應用程序類
QApplication a(argc, argv);
//MyWidget對象
Widget w;
w.show();
//程序進入消息循環
return a.exec();
}
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;
}
widget.h
頭文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
QT的所有代碼都已經同步在gitee中,並持續更新。
@Author: cpu_code
@Date: 2020-06-11 20:24:20
@LastEditTime: 2020-06-11 20:26:09
@FilePath: \QT\firstQt\main.cpp
@Gitee: https://gitee.com/cpu_code
@CSDN: https://blog.csdn.net/qq_44226094