switchbutton.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef SWITCHBUTTON_H
  2. #define SWITCHBUTTON_H
  3. /**
  4. * 开关按钮控件 作者:feiyangqingyun(QQ:517216493) 2016-11-6
  5. * 1:可设置开关按钮的样式 圆角矩形/内圆形/外圆形
  6. * 2:可设置选中和未选中时的背景颜色
  7. * 3:可设置选中和未选中时的滑块颜色
  8. * 4:可设置显示的文本
  9. * 5:可设置滑块离背景的间隔
  10. * 6:可设置圆角角度
  11. * 7:可设置是否显示动画过渡效果
  12. */
  13. #include <QWidget>
  14. #include <QPainterPath>
  15. #ifdef quc
  16. #if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
  17. #include <QtDesigner/QDesignerExportWidget>
  18. #else
  19. #include <QtUiPlugin/QDesignerExportWidget>
  20. #endif
  21. class QDESIGNER_WIDGET_EXPORT SwitchButton : public QWidget
  22. #else
  23. class SwitchButton : public QWidget
  24. #endif
  25. {
  26. Q_OBJECT
  27. Q_ENUMS(ButtonStyle)
  28. Q_PROPERTY(int space READ getSpace WRITE setSpace)
  29. Q_PROPERTY(int rectRadius READ getRectRadius WRITE setRectRadius)
  30. Q_PROPERTY(bool checked READ getChecked WRITE setChecked)
  31. Q_PROPERTY(bool showText READ getShowText WRITE setShowText)
  32. Q_PROPERTY(bool showCircle READ getShowCircle WRITE setShowCircle)
  33. Q_PROPERTY(bool animation READ getAnimation WRITE setAnimation)
  34. Q_PROPERTY(ButtonStyle buttonStyle READ getButtonStyle WRITE setButtonStyle)
  35. Q_PROPERTY(QColor bgColorOff READ getBgColorOff WRITE setBgColorOff)
  36. Q_PROPERTY(QColor bgColorOn READ getBgColorOn WRITE setBgColorOn)
  37. Q_PROPERTY(QColor sliderColorOff READ getSliderColorOff WRITE setSliderColorOff)
  38. Q_PROPERTY(QColor sliderColorOn READ getSliderColorOn WRITE setSliderColorOn)
  39. Q_PROPERTY(QColor textColorOff READ getTextColorOff WRITE setTextColorOff)
  40. Q_PROPERTY(QColor textColorOn READ getTextColorOn WRITE setTextColorOn)
  41. Q_PROPERTY(QString textOff READ getTextOff WRITE setTextOff)
  42. Q_PROPERTY(QString textOn READ getTextOn WRITE setTextOn)
  43. public:
  44. enum ButtonStyle {
  45. ButtonStyle_Rect = 0, //圆角矩形
  46. ButtonStyle_CircleIn = 1, //内圆形
  47. ButtonStyle_CircleOut = 2 //外圆形
  48. };
  49. SwitchButton(QWidget *parent = 0);
  50. ~SwitchButton();
  51. protected:
  52. void mousePressEvent(QMouseEvent *);
  53. void resizeEvent(QResizeEvent *);
  54. void paintEvent(QPaintEvent *);
  55. void drawBg(QPainter *painter);
  56. void drawSlider(QPainter *painter);
  57. private:
  58. int space; //滑块离背景间隔
  59. int rectRadius; //圆角角度
  60. bool checked; //是否选中
  61. bool showText; //显示文字
  62. bool showCircle; //显示小圆
  63. bool animation; //动画过渡
  64. ButtonStyle buttonStyle; //开关按钮样式
  65. QColor bgColorOff; //关闭时背景颜色
  66. QColor bgColorOn; //打开时背景颜色
  67. QColor sliderColorOff; //关闭时滑块颜色
  68. QColor sliderColorOn; //打开时滑块颜色
  69. QColor textColorOff; //关闭时文字颜色
  70. QColor textColorOn; //打开时文字颜色
  71. QString textOff; //关闭时显示的文字
  72. QString textOn; //打开时显示的文字
  73. int step; //每次移动的步长
  74. int startX; //滑块开始X轴坐标
  75. int endX; //滑块结束X轴坐标
  76. QTimer *timer; //定时器绘制
  77. private slots:
  78. void change();
  79. void updateValue();
  80. public:
  81. int getSpace() const;
  82. int getRectRadius() const;
  83. bool getChecked() const;
  84. bool getShowText() const;
  85. bool getShowCircle() const;
  86. bool getAnimation() const;
  87. ButtonStyle getButtonStyle() const;
  88. QColor getBgColorOff() const;
  89. QColor getBgColorOn() const;
  90. QColor getSliderColorOff() const;
  91. QColor getSliderColorOn() const;
  92. QColor getTextColorOff() const;
  93. QColor getTextColorOn() const;
  94. QString getTextOff() const;
  95. QString getTextOn() const;
  96. QSize sizeHint() const;
  97. QSize minimumSizeHint() const;
  98. public Q_SLOTS:
  99. //设置间隔
  100. void setSpace(int space);
  101. //设置圆角角度
  102. void setRectRadius(int rectRadius);
  103. //设置是否选中
  104. void setChecked(bool checked);
  105. //设置是否显示文字
  106. void setShowText(bool showText);
  107. //设置是否显示小圆
  108. void setShowCircle(bool showCircle);
  109. //设置是否动画过渡
  110. void setAnimation(bool animation);
  111. //设置风格样式
  112. void setButtonStyle(const ButtonStyle &buttonStyle);
  113. //设置背景颜色
  114. void setBgColorOff(const QColor &bgColorOff);
  115. void setBgColorOn(const QColor &bgColorOn);
  116. //设置滑块颜色
  117. void setSliderColorOff(const QColor &sliderColorOff);
  118. void setSliderColorOn(const QColor &sliderColorOn);
  119. //设置文字颜色
  120. void setTextColorOff(const QColor &textColorOff);
  121. void setTextColorOn(const QColor &textColorOn);
  122. void enableText(bool enable);
  123. //设置文字
  124. void setTextOff(const QString &textOff);
  125. void setTextOn(const QString &textOn);
  126. Q_SIGNALS:
  127. void checkedChanged(bool checked);
  128. };
  129. #endif // SWITCHBUTTON_H