(Qt) 默認QtWidget應用包含什么?

文章目錄

  • ?前言
  • ?創建
    • 🛠?選擇一個模板
    • 🛠?Location
    • 🛠?構建系統
    • 🛠?Details
    • 🛠?Translation
    • 🛠?構建套件(Kit)
    • 🛠?匯總
  • ?項目
    • ??概要
    • ??構建步驟
    • ??清除步驟
  • ?Code
    • 🔦untitled.pro
    • 🔦main.cpp
    • 🔦mainwindow.h
    • 🔦mainwindow.cpp
    • 🔦mainwindow.ui
  • END
    • ?視頻講解

?前言

本文將帶大家,查看一個默認的Qt Widget程序可能會涉及哪些方面的內容。

本文默認使用IDE為Qt Creator和qmake編譯套件。

當然由于個人能力和水平的局限性,并不可能帶領大家看到所有的全貌。

?創建

🛠?選擇一個模板

選擇 Application (Qt)并選擇一個窗口應用Qt Widgets Application

在這里插入圖片描述

🛠?Location

選擇一個項目生成的位置,注意,不要出現中文路徑。

這里的名稱默認是untitled,大家可以改成自己項目的名稱。也不要有中文。

在這里插入圖片描述

🛠?構建系統

選擇qmake,當然目前Qt6在大力推行cmake進行編譯。

本文這里以傳統的qmake為例。

在這里插入圖片描述

而最后的Qbs是qt提出的另一種構建方式

但是在2018年宣布正式棄用:Deprecation of Qbs (qt.io)

在這里插入圖片描述

🛠?Details

這里是生成默認代碼的配置。

默認情況下,頭文件,源文件,ui文件三者會跟隨類名。這里默認使用的是QMainWindow

繼承關系鏈 QMainWindow -> QWidget -> QObject

在這里插入圖片描述

🛠?Translation

翻譯文件,國際化翻譯配置,一般都是根據需要生成。這里默認無即可。

在這里插入圖片描述

🛠?構建套件(Kit)

這里Qt Creator會根據安裝時的qt版本顯示。這里隨意選擇即可,后面還可以自己再選擇。

在這里插入圖片描述

🛠?匯總

項目管理,這里沒有子項目。因此直接默認。

一般這些東西都是在項目過程中手動管理進去的。

在這里插入圖片描述

?項目

在這里插入圖片描述

??概要

構建目錄

這里的構建目錄會默認生成在.pro文件所在文件夾的同級中。

里面包含了,qt版本,編譯器,debug-release等等信息。

C:\Users\lotus\Desktop\build-untitled-Desktop_Qt_5_15_2_MinGW_32_bit-Debug

??構建步驟

在構建目錄中,分別執行qmake指令和make指令,即可編譯好我們的Qt程序。

當然下面并非是真正的執行指令,而是一些顯示的配置參數。

qmake

D:/Qt/5.15.2/mingw81_32/bin/qmake.exe C:\Users\lotus\Desktop\untitled\untitled.pro -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug" && D:/Qt/Tools/mingw810_32/bin/mingw32-make.exe qmake_all

Make

mingw32-make.exe -j16 in C:\Users\lotus\Desktop\build-untitled-Desktop_Qt_5_15_2_MinGW_32_bit-Debug

??清除步驟

刪除一些中間文件,比如.o,moc_等等文件,其中文件夾不刪除。

mingw32-make.exe clean -j16 in C:\Users\lotus\Desktop\build-untitled-Desktop_Qt_5_15_2_MinGW_32_bit-Debug

?Code

🔦untitled.pro

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += 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.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \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

QT

Qt的核心庫,如core,gui,widgets等等

CONFIG

添加相關配置,比如這里希望以c++17的標準編譯。

DEFINES

添加宏定義,此處的QT_DISABLE_DEPRECATED_BEFORE=0x060000。表示如果使用廢棄的接口函數,則將無法編譯。

如果不指定值,則默認值為1。

SOURCES

原文件

HEADERS

頭文件

FORMS

ui文件

分支判斷

qnx: 表示在qnx的環境下。

else:類似if-else

當然這里也可以添加{}塊作用域。

qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

🔦main.cpp

#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

QApplication

是整個程序的核心,管理了qt的所有主事件循環,用exec()進入阻塞等待。

當退出時,返回一個int。

w.show()

這里建立一個ui組件,并顯示。

當最后一個ui關閉時,則退出主時間循環,即exec()結束,并返回。

🔦mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private:Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

元對象

繼承自QObject,并包含宏Q_OBJECT

繼承關系:QMainWindow -> QWidget -> QObject

將該文件通過moc解析。

moc.exe mainwindow.h -o mainwindow.moc.cpp

mainwindow.moc.cpp

/****************************************************************************
** Meta object code from reading C++ file 'mainwindow.h'
**
** Created by: The Qt Meta Object Compiler version 68 (Qt 6.2.4)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/#include <memory>
#include "mainwindow.h"
#include <QtCore/qbytearray.h>
#include <QtCore/qmetatype.h>
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'mainwindow.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 68
#error "This file was generated using the moc from 6.2.4. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endifQT_BEGIN_MOC_NAMESPACE
QT_WARNING_PUSH
QT_WARNING_DISABLE_DEPRECATED
struct qt_meta_stringdata_MainWindow_t {const uint offsetsAndSize[2];char stringdata0[11];
};
#define QT_MOC_LITERAL(ofs, len) \uint(offsetof(qt_meta_stringdata_MainWindow_t, stringdata0) + ofs), len 
static const qt_meta_stringdata_MainWindow_t qt_meta_stringdata_MainWindow = {{
QT_MOC_LITERAL(0, 10) // "MainWindow"},"MainWindow"
};
#undef QT_MOC_LITERALstatic const uint qt_meta_data_MainWindow[] = {// content:10,       // revision0,       // classname0,    0, // classinfo0,    0, // methods0,    0, // properties0,    0, // enums/sets0,    0, // constructors0,       // flags0,       // signalCount0        // eod
};void MainWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)
{(void)_o;(void)_id;(void)_c;(void)_a;
}const QMetaObject MainWindow::staticMetaObject = { {QMetaObject::SuperData::link<QMainWindow::staticMetaObject>(),qt_meta_stringdata_MainWindow.offsetsAndSize,qt_meta_data_MainWindow,qt_static_metacall,nullptr,
qt_incomplete_metaTypeArray<qt_meta_stringdata_MainWindow_t
, QtPrivate::TypeAndForceComplete<MainWindow, std::true_type>>,nullptr
} };const QMetaObject *MainWindow::metaObject() const
{return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;
}void *MainWindow::qt_metacast(const char *_clname)
{if (!_clname) return nullptr;if (!strcmp(_clname, qt_meta_stringdata_MainWindow.stringdata0))return static_cast<void*>(this);return QMainWindow::qt_metacast(_clname);
}int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{_id = QMainWindow::qt_metacall(_c, _id, _a);return _id;
}
QT_WARNING_POP
QT_END_MOC_NAMESPACE

namespace Ui { class MainWindow; }

是ui文件中,頂層對象名稱就是這里的類名。可以通過解析ui文件進行查看。

當然這里是一個典型的Pimpl技巧。

🔦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;
}

該文件核心是查看這兩個頭文件。

#include "mainwindow.h"
#include "ui_mainwindow.h"

<ui_mainwindow.h> 是通過ui文件生成的C++代碼。(將xml轉化為cpp代碼)

🔦mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"/><widget class="QMenuBar" name="menubar"/><widget class="QStatusBar" name="statusbar"/></widget><resources/><connections/>
</ui>

xml

顯然易見的,ui文件的本質是一個xml文件。

uic

可以通過uic指令將ui文件轉化為cpp代碼。

uic mainwindow.ui -o ui_mainwindow.h

ui_mainwindow.h

/********************************************************************************
** Form generated from reading UI file 'mainwindow.ui'
**
** Created by: Qt User Interface Compiler version 6.2.4
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/#ifndef UI_MAINWINDOW_H
#define UI_MAINWINDOW_H#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QWidget>QT_BEGIN_NAMESPACEclass Ui_MainWindow
{
public:QWidget *centralwidget;QMenuBar *menubar;QStatusBar *statusbar;void setupUi(QMainWindow *MainWindow){if (MainWindow->objectName().isEmpty())MainWindow->setObjectName(QString::fromUtf8("MainWindow"));MainWindow->resize(800, 600);centralwidget = new QWidget(MainWindow);centralwidget->setObjectName(QString::fromUtf8("centralwidget"));MainWindow->setCentralWidget(centralwidget);menubar = new QMenuBar(MainWindow);menubar->setObjectName(QString::fromUtf8("menubar"));MainWindow->setMenuBar(menubar);statusbar = new QStatusBar(MainWindow);statusbar->setObjectName(QString::fromUtf8("statusbar"));MainWindow->setStatusBar(statusbar);retranslateUi(MainWindow);QMetaObject::connectSlotsByName(MainWindow);} // setupUivoid retranslateUi(QMainWindow *MainWindow){MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));} // retranslateUi};namespace Ui {class MainWindow: public Ui_MainWindow {};
} // namespace UiQT_END_NAMESPACE#endif // UI_MAINWINDOW_H



END

?視頻講解

視頻講解參見

關注我,學習更多C/C++,算法,計算機知識

B站:

👨?💻主頁:天賜細蓮 bilibili

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/14803.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/14803.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/14803.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【EasyX】快速入門——消息處理,音頻

1.消息處理 我們先看看什么是消息 1.1.獲取消息 想要獲取消息,就必須學會getmessage函數 1.1.1.getmessage函數 有兩個重載版本,它們的作用是一樣的 參數filter可以篩選我們需要的消息類型 我們看看參數filter的取值 當然我們可以使用位運算組合這些值 例如,我們…

華為CE6851-48S6Q-HI升級設備版本及補丁

文章目錄 升級前準備工作筆記本和交換機設備配置互聯地址啟用FTP設備訪問FTP設備升級系統版本及補丁 升級前準備工作 使用MobaXterm遠程工具連接設備&#xff0c;并作為FTP服務器準備升級所需的版本文件及補丁文件 筆記本和交換機設備配置互聯地址 在交換機接口配置IP&#…

Facebook隱私保護:數據安全的前沿挑戰

在數字化時代&#xff0c;隨著社交媒體的普及和應用&#xff0c;個人數據的隱私保護問題日益受到關注。作為全球最大的社交平臺之一&#xff0c;Facebook承載了數十億用戶的社交活動和信息交流&#xff0c;但與此同時&#xff0c;也面臨著來自內外部的數據安全挑戰。本文將深入…

AWS Elastic Beanstalk 監控可觀測最佳實踐

一、概述 Amazon Web Services (AWS) 包含一百多種服務&#xff0c;每項服務都針對一個功能領域。服務的多樣性可讓您靈活地管理 AWS 基礎設施&#xff0c;然而&#xff0c;判斷應使用哪些服務以及如何進行預配置可能會非常困難。借助 Elastic Beanstalk&#xff0c;可以在 AW…

【LinuxC語言】一切皆文件的理念

文章目錄 引言一、什么是“一切皆文件”&#xff1f;1. 文件柜的類比2. 統一的操作方式3. 舉個具體例子4. 設備文件5. 進程和網絡連接6. 簡化管理 二、這一設計的優勢1. 統一接口2. 靈活性3. 簡化了系統管理4. 增強了系統安全性 結論 引言 Linux 操作系統以其獨特的設計理念和…

如何使用JMeter 進行全鏈路壓測

使用 JMeter 進行全鏈路壓測&#xff1a;詳細步驟指南 全鏈路壓測旨在測試整個系統的性能&#xff0c;包括所有的組件和服務。通過 Apache JMeter 進行全鏈路壓測&#xff0c;可以模擬真實用戶行為&#xff0c;測試系統在高負載下的表現。以下是詳細的步驟指南&#xff0c;分為…

AWTK實現汽車儀表Cluster/DashBoard嵌入式GUI開發(七):快啟

前言: 汽車儀表是人們了解汽車狀況的窗口,而儀表中的大部分信息都是以指示燈形式顯示給駕駛者。儀表指示燈圖案都較為抽象,對駕駛不熟悉的人在理解儀表指示燈含義方面存在不同程度的困難,尤其對于駕駛新手,如果對指示燈的含義不求甚解,有可能影響駕駛的安全性。即使是對…

Pytest框架實戰二

在Pytest框架實戰一中詳細地介紹了Pytest測試框架在參數化以及Fixture函數在API測試領域的實戰案例以及具體的應用。本文章接著上個文章的內容繼續闡述Pytest測試框架優秀的特性以及在自動化測試領域的實戰。 conftest.py 在上一篇文章中闡述到Fixture函數的特性&#xff0c;第…

shell循環

一、for循環 用法&#xff1a; for 變量 in 取值列表 do 命令序列 done 例1&#xff1a;打印1到10的數字列表 #!/bin/bashfor i in {1..10} do echo $i done 例2&#xff1a;#批量添加用戶,用戶名存放在users.txt文件中&#xff0c;每行一個,初始密碼均設為123456 #!/bin/bas…

KMP算法【C++】

KMP算法測試 KMP 算法詳解 根據解釋寫出對應的C代碼進行測試&#xff0c;也可以再整理成一個函數 #include <iostream> #include <vector>class KMP { private:std::string m_pat;//被匹配的字符串std::vector<std::vector<int>> m_dp;//狀態二維數組…

怎樣解決Redis高并發競爭Key難點?

Redis作為一種高性能的鍵值存儲系統&#xff0c;在現代分布式系統中發揮著重要作用。然而&#xff0c;高并發場景下對同一Key的操作可能引發競爭條件&#xff0c;給系統穩定性和數據一致性帶來挑戰。本文將探討如何解決這一問題&#xff0c;為讀者提供有效的應對策略。 1. Red…

【002】FlexBison實現原理

0. 前言 Flex和Bison是用于構建處理結構化輸入的程序的工具。它們最初是用于構建編譯器的工具&#xff0c;但它們已被證明在許多其他領域都很有用。 &#xfeff; 在第一章中&#xff0c;我們將首先看一點(但不是太多)它們背后的理論&#xff0c;然后我們將深入研究一些使用它…

Mysql和Postgresql創建用戶和授權命令

Mysql和Postgresql創建用戶和授權命令 MySQL/MariaDB/TiDB mysql -uroot -P3306 -p 輸入密碼&#xff1a;xxx create user user1% identified by xxx; grant all privileges on *.* to user1%; create user user2% identified by xxx; grant all privileges on *.* to user2%;…

Winform /C# 截圖當前窗體,指定區域,當前屏幕

1.當前窗體 public static Image CaptureControl(Control ctrl){System.Drawing.Bitmap bmp new System.Drawing.Bitmap(ctrl.Width, ctrl.Height);ctrl.DrawToBitmap(bmp, new Rectangle(0, 0, ctrl.Width, ctrl.Height));return bmp;}private void DownLoad(){string filePa…

java類中運行main方法時報錯:找不到或無法加載主類 XXX

運行main類報了這個錯 錯誤: 找不到或無法加載主類 XXX 經過好一番查證才找出了問題所在 原因是 maven項目的provided導致的&#xff0c;現在記錄一下。 將pom.xml中標注provided的注釋掉&#xff0c;就不報錯了。

ERROR [internal] load metadata for docker.io/library/node:20-alpine

docker編譯時報錯&#xff0c;除標題外&#xff0c;還報如下信息 ERROR: failed to solve: node:20-alpine: failed to resolve source metadata for docker.io/library/node:20-alpine: failed to do request: Head "https://registry-1.docker.io/v2/library/node/mani…

常用個人信息

目錄 常用聯系方式我的自動思維常用媒體專業相關康米相關黑歷史 常用聯系方式 QQ&#xff1a;2868679921 微信&#xff1a;Commieee 郵箱&#xff1a;sharvefoxmail.com 我的自動思維 常用媒體 嗶哩嗶哩 專業相關 博客 康米相關 QQ&#xff1a;1203361015 黑歷史 貼吧…

PyQt5學習系列之QMetaObject.connectSlotsByName

文章目錄 前言一、pandas是什么&#xff1f;二、使用步驟 1.引入庫2.讀入數據總結 學習記錄 QMetaObject.connectSlotsByName——自動將信號連接到槽&#xff08;函數&#xff09; 例如&#xff1a; from PyQt5.QtWidgets import QMainWindow, QPushButton from PyQt5.QtCore…

哪些類型的產品適合用3D形式展示?

隨著3D技術的蓬勃發展&#xff0c;眾多品牌和企業紛紛投身3D數字化浪潮&#xff0c;將產品打造成逼真的3D模型進行展示&#xff0c;消費者可以更加直觀地了解產品的特點和優勢&#xff0c;從而做出更明智的購買決策。 哪些產品適合3D交互展示&#xff1f; 產品3D交互展示具有直…

2024系統架構師--- 希賽模擬答案知識點

案例第一題&#xff1a; MVC架構包含&#xff1a;視圖、控制器、模型&#xff1b; 視圖&#xff08;View&#xff09;&#xff1a;視圖是用戶看到并與之交互的界面。視圖面向用戶顯示相關的數據&#xff0c;并能接收用戶的輸入數據&#xff0c;但是它并不能進行任何實際的業務…