QT學習記錄 自定義開關按鈕

2021-10-01 18:04:38 字數 1542 閱讀 3151

簡單的按鈕開關實現

自定義類派生於qwidget-在上面畫2個圓形和乙個矩形,合成乙個開關樣式,再讓其中乙個圓形填充不一樣的顏色就好了。

switchbutton.h

#ifndef switchbutton_h

#define switchbutton_h

#include #include #include #include #include class switchbutton : public qwidget

;#endif // switchbutton_h

switchbutton.cpp

#include "switchbutton.h"

switchbutton::switchbutton(qwidget *parent) : qwidget(parent),haschecked(false)

void switchbutton::paintevent(qpaintevent *)

else

}void switchbutton::mousereleaseevent(qmouseevent *)

然後在需要的地方定義乙個switchbutton就出現了乙個簡陋的開關,在沒有特殊要求的情況下,已滿足要求,如下:

widget.h

#ifndef widget_h

#define widget_h

#include namespace ui

class widget : public qwidget

;#endif // widget_h

widget.cpp

#include

"widget.h"

#include

"ui_widget.h"

#include

"switchbutton.h"

#include

widget::

widget

(qwidget *parent)

:qwidget

(parent),ui

(new ui::widget));

}widget::

~widget()

main.c

#include

"widget.h"

#include

intmain

(int argc,

char

*ar**)

效果如下

Qt之自定義控制項(開關按鈕)

接觸過ios系統的童鞋們應該對開關按鈕很熟悉,在設定裡面經常遇到,切換時候的滑動效果比較帥氣。通常說的開關按鈕,有兩個狀態 on off。下面,我們利用自定義控制項來實現乙個開關按鈕。更多參考 其餘介面用於擴充套件,也可自己擴充。switchcontrol.h ifndef switch contr...

Qt 自定義按鈕

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

Android 自定義開關按鈕樣式

效果圖 1,定義乙個drawable shape thumb off.xml檔案,定義開關關閉時,thumb滑塊顯示的效果。1.0 encoding utf 8 xmlns android android shape rectangle android radius 50dp android hei...