Qt自定義sink風格按鈕

2021-10-07 05:58:54 字數 2131 閱讀 5199

為了滿足工作中控制項多樣性的要求,一些控制項的風格需要通過重寫畫家事件的方式進行自定義;

實現sink風格按鈕的效果圖如下:

純**實現思路:

產生按鈕下沉的效果是利用了視覺誤差原理,通過更改背景顏色以及圖示的位置,呈現出按鈕動態改變的效果。

按鈕未按下狀態繪製**:

void sinkbutton::

drawunsinkrect

(qpainter *painter)

按鈕按下狀態**:

void sinkbutton::

drawsinkedrect

(qpainter *painter)

完整的原始碼:

sinkbutton.h

#ifndef sinkbutton_h

#define sinkbutton_h

#include

#include

class

sinkbutton

:public qpushbutton

;#endif

// sinkbutton_h

sinkbutton.cpp

#include

"sinkbutton.h"

#include

#include

sinkbutton::

sinkbutton

(qwidget *parent)

:qpushbutton

(parent)

sinkbutton::

~sinkbutton()

void sinkbutton::

mousepressevent

(qmouseevent *event)

void sinkbutton::

mousereleaseevent

(qmouseevent *event)

void sinkbutton::

paintevent

(qpaintevent *event)

else

}else

drawicon

(&painter)

;drawtext

(&painter);}

void sinkbutton::

drawunsinkrect

(qpainter *painter)

void sinkbutton::

drawsinkedrect

(qpainter *painter)

void sinkbutton::

drawdisblerect

(qpainter *painter)

void sinkbutton::

drawinerrect

(qpainter *painter)

void sinkbutton::

drawtext

(qpainter *painter)

else

painter-

>

restore()

;}void sinkbutton::

drawicon

(qpainter *painter)

else

painter-

>

restore()

;}void sinkbutton::

seticon

(const qstring &iconname)

void sinkbutton::

settext

(const qstring &text)

void sinkbutton::

setpressbgcolor

(const qcolor& pressbgcolor)

void sinkbutton::

setreleabgcolor

(const qcolor& releabgcolor)

Qt 自定義按鈕

自定義控制項的實現思路如下 a1.新建乙個類,該類繼承qpushbutton,由於qpushbutton繼承於qwidget,因此可以直接在該繼承類裡面進行布局管理和掛載控制項 a2.新建兩個qlabel例項,即buttonimage和buttontxt 是qlable例項 分別用兩個垂直布局管理器...

Qt自定義標籤按鈕

當你接觸到qt時,你會為它極為方便的跨平台方面感到吃驚,從而想嘗試著使用qt。漸漸地你會發現qt自帶的一些控制項不能滿足自己的需要,此時就需要我們自己定義乙個屬於自己的控制項。總所周知,標籤的風格設定模擬較多,但預設的標籤沒有滑鼠響應事件。在你的qt工程中 新增新檔案 c class,輸入類名 cl...

Qt 控制項 自定義按鈕

mybutton.h ifndef mybutton h define mybutton h include include include include class mybutton public qwidget endif mybutton h mybutton.cpp include myb...