Qt中Widget Aplication项目的文件内容介绍
生活随笔
收集整理的這篇文章主要介紹了
Qt中Widget Aplication项目的文件内容介绍
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
項(xiàng)目創(chuàng)建完成之后會出現(xiàn)如下界面:
之后對項(xiàng)目中的各個文件內(nèi)容進(jìn)行介紹
1 001.pro文件。一般項(xiàng)目中的.pro 文件是項(xiàng)目的項(xiàng)目文件
QT += core gui #Qt包含的模塊greaterThan(QT_MAJOR_VERSION, 4): QT += widgets #大于4版本 包含widget模塊CONFIG += c++11# The following define makes your compiler emit warnings if you use # any Qt feature that has been marked 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 it uses 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.0SOURCES += \ #包含源文件main.cpp \mybutton.cpp \mywidget.cppHEADERS += \mybutton.h \mywidget.h# Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target2、對項(xiàng)目中的main.cpp文件介紹,這是整個項(xiàng)目的開始位置
#include "mywidget.h"#include <QApplication>//包含頭文件 應(yīng)用程序 //程序的入口 argc為命令行的數(shù)量 argv是命令行變量的數(shù)組 int main(int argc, char *argv[]) {//應(yīng)用程序?qū)ο?a,Qt中有且僅有一個應(yīng)用程序?qū)ο驫Application a(argc, argv);//創(chuàng)建MyWidget對象 w, MyWidget基類是 QWidgetMyWidget w;//窗口一般默認(rèn)是不會彈出的,如果想要彈出就需要調(diào)用 show方法w.show();return a.exec();//a.exec()是進(jìn)行消息循環(huán)機(jī)制,相當(dāng)于pause}3、介紹項(xiàng)目中創(chuàng)建的時候,創(chuàng)建的一個繼承于QWidget的mainWindow類
1).h文件
#ifndef MYWIDGET_H #define MYWIDGET_H#include <QWidget>class MyWidget : public QWidget //MyWidget繼承QWidget類 {Q_OBJECT //Q_OBJECT是Qt中的一個宏,寫了這個宏就說明支持Qt中的信號和槽機(jī)制public:MyWidget(QWidget *parent = nullptr);//構(gòu)造函數(shù)~MyWidget();//析構(gòu)函數(shù) }; #endif // MYWIDGET_H2).cpp文件
#include "mywidget.h"#include "mybutton.h"MyWidget::MyWidget(QWidget *parent): QWidget(parent)//初始化列表 {}MyWidget::~MyWidget() { }注意:其他后面接收的類除了繼承的父類不同,其他的結(jié)構(gòu)和這個類一樣。
介紹完基本的一些文件類容之后,下面以在界面創(chuàng)建一個按鈕控件鏡像進(jìn)一步了解。此內(nèi)容繼以上文件繼續(xù)書寫。在mianWindow類中增加按鈕函數(shù)。
#include "mywidget.h" #include <QPushButton> #include "mybutton.h" MyWidget::MyWidget(QWidget *parent): QWidget(parent)//初始化列表 {//創(chuàng)建一個按鈕 //1)第一種方式//按鈕 使用的類為QPushButtonQPushButton *pushButton = new QPushButton;//創(chuàng)建一個按鈕控件pushButton->show();//show是用頂層方式的彈出,如果想在MyWidget窗口中顯示,就需要依賴MyWidget窗口//設(shè)置父親pushButton->setParent(this);//設(shè)置pushButton按鈕的父窗口為當(dāng)前widgetpushButton->setText(tr("赤水"));//設(shè)置按鈕名稱 //第二種方式QPushButton *pushButton1 = new QPushButton(tr("南北將軍"),this);//在定義的時候指定控件的父窗口和控件名稱//重置窗口大小resize(600,400);//移動第二個按鈕pushButton1->resize(50,50);//重置窗口標(biāo)題setWindowTitle(tr("Qt第一個窗口"));//重置窗口大小setFixedSize(600,400);//一定程度下 簡化了內(nèi)存回收機(jī)制//創(chuàng)建自己的按鈕MyButton *myBtn = new MyButton;myBtn->setText(tr("我的按鈕"));myBtn->move(200,200);myBtn->setParent(this);//Qt 坐標(biāo)系//x為右側(cè)正向 y以下側(cè)為正向//點(diǎn)擊myBtn關(guān)閉窗口//使用connect函數(shù) 參數(shù)1 信號發(fā)送者 參數(shù)2 發(fā)送的信號(信號地址) 參數(shù)3 信號的接收者 參數(shù)4 處理的槽函數(shù)(函數(shù)地址)connect(myBtn,&MyButton::clicked,this,&QWidget::close);}MyWidget::~MyWidget() { }最后在介紹幾個Qt中的一些快捷鍵
| 功能 | 快捷鍵 |
| 運(yùn)行 | ctrl+R |
| 編譯 | ctrl+B |
| 查找 | ctrl+F |
| 幫助文檔 | F1 |
| 字體縮放 | ctrl+鼠標(biāo)滾輪 |
| 自動對齊 | ctrl+i |
| 整行移動 | ctrl+shift+t 或者 i? |
| 同名之間的.h和.cpp文件之間切換 | F4 |
| 注釋快捷鍵 | ctrl+/ |
?
總結(jié)
以上是生活随笔為你收集整理的Qt中Widget Aplication项目的文件内容介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android FM模块学习之四源码解析
- 下一篇: 十进制与二进制的相互转换