1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#ifndef TOGGLESWITCH_H
#define TOGGLESWITCH_H
#include <QCheckBox>
#include <QPainter>
#include <QPropertyAnimation>
class ToggleSwitch : public QCheckBox
{
Q_OBJECT
Q_PROPERTY(float handlePosition READ handlePosition WRITE setHandlePosition NOTIFY handlePositionChanged)
public:
explicit ToggleSwitch(QWidget *parent = nullptr,
QColor barColor = QColor(242, 145, 58), // Replace "#F2913A"
QColor checkedColor = QColor(242, 145, 58), // Replace "#F2913A"
QColor handleColor = QColor(252, 241, 230),
float hScale = 1.0f,
float vScale = 1.1f,
int fontSize = 9);
QSize sizeHint() const override;
bool hitButton(const QPoint &pos) const override;
float handlePosition() const { return m_handlePosition; }
void setHandlePosition(float pos);
void setHScale(float value);
void setVScale(float value);
void setFontSize(int value);
signals:
void handlePositionChanged(float position);
protected:
void paintEvent(QPaintEvent *e) override;
private slots:
void handleStateChange(int value);
private:
QBrush m_barBrush;
QBrush m_barCheckedBrush;
QBrush m_handleBrush;
QBrush m_handleCheckedBrush;
float m_handlePosition;
float m_hScale;
float m_vScale;
int m_fontSize;
};
#endif // TOGGLESWITCH_H
|