1 /*! 2 * @file switchbutton.h 3 * Copyright (c) 2018 4 * @brief des 5 * detailed des 6 * 7 * @date 2018 8 * @author lee 9 */ 10 #ifndef SWITCHBUTTON_H 11 #define SWITCHBUTTON_H 12 13 #include <QWidget> 14 #include <QTimer> 15 class SwitchButton : public QWidget 16 { 17 Q_OBJECT 18 public: 19 explicit SwitchButton(QWidget *parent = 0); 20 //return status -- open: true close: false 21 bool isToggled() const; 22 //set switch status 23 void setToggled(bool checked); 24 //set background color 25 void setBackgroundColor(QColor color); 26 //set checked color 27 void setCheckedColor(QColor color); 28 //set disable color 29 void setDisabledColor(QColor color); 30 protected: 31 //paintevent 32 void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; 33 //mouseEvents 34 void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; 35 36 void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; 37 //size change event 38 void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; 39 //default size 40 QSize sizeHint() const Q_DECL_OVERRIDE; 41 42 QSize minimumSizeHint() const Q_DECL_OVERRIDE; 43 44 signals: 45 //status change signal 46 void toggled(bool checked); 47 48 public slots: 49 //for switch animation 50 void onTimeout(); 51 private: 52 bool m_bChecked; //checked or not 53 QColor m_backgroundColor; //background color 54 QColor m_CheckedColor; //checed color 55 QColor m_disableColor; //disabled color 56 QColor m_thumbColorOn; // 57 QColor m_thumbColorOff; 58 qreal m_radius; // 59 qreal m_nX; // x 60 qreal m_nY; // y 61 qint16 m_nHeight; //height 62 qint16 m_nMargin; //margin 63 QTimer m_timer; //timer 64 }; 65 66 #endif // SWITCHBUTTON_H 67