Qt入門(1) 建立視窗和按鈕

2021-10-01 08:51:19 字數 1595 閱讀 8808

1 建立主視窗widget

2 在視窗中建立button

3 對button物件新增父類和子類設定(分別通過物件成員函式以及建構函式)

4 對button使用訊號和槽的概念,了解connet的機制

subwidget.h

#ifndef subwidget_h

#define subwidget_h

#include #includeclass subwidget : public qwidget

;#endif // subwidget_h

mainwidget.h
#ifndef mainwidget_h

#define mainwidget_h

#include #includeclass mainwidget : public qwidget

;#endif // mainwidget_h

main.cpp
#include "mainwidget.h"

mainwidget.cpp
#include "mainwidget.h"

mainwidget::mainwidget(qwidget *parent)

: qwidget(parent)

mainwidget::~mainwidget()

void mainwidget::mainwidget2sub()

void mainwidget::sub2mainwidget()

subwidget.cpp
#include "subwidget.h"

subwidget::subwidget(qwidget *parent) : qwidget(parent)

void subwidget::sendsignal()

connect(&b1,&qpushbutton::pressed,this,&mainwidget::close);

&b1,訊號發出者的指標型別

&qpushbutton::pressed,處理的訊號;&傳送者的類名::訊號名字

this, 訊號接收者

&mainwidget::close,槽函式/訊號處理函式,&接收者類名::槽函式名字

槽函式需要有和訊號一致(引數,返回值)

槽函式要使用和接受者一樣的類名, 並且自定義槽函式要在接收者的header檔案中進行宣告

qt中所有的類都是由header和cpp方法檔案組成的

void mysignal();//自定義訊號沒有返回值,但是可以有引數

傳送訊號只需要emit signal();

Qt 設定和建立按鈕,視窗

main.cpp int main int argc,char ar widget.cpp widget widget qwidget parent qwidget parent ui new ui widget 影響的是widget視窗的大小 此時視窗無法再進行拖拽大小了 qpushbutton ...

06 QT 按鈕建立

在qt程式中,最常用的控制項之一就是按鈕了,首先我們來看下如何建立乙個按鈕 qpushbutton btn new qpushbutton 標頭檔案 include 設定父親 btn setparent this 設定文字 btn settext 德瑪西亞 移動位置 btn move 100 100...

Qt設定視窗 按鈕背景色

1 設定視窗背景色時使用如下樣式表 background image url img png bj png 使用後如果背景大小不夠會被平鋪。qpushbutton qpushbutton hover 如果視窗已設定背景,則會出現類似父視窗設定時,如果子視窗未設定相應的屬性覆蓋,則子視窗會使用父視窗對...