#ifndef SWITCHFORM_H #define SWITCHFORM_H

#include #include #include

namespace Ui { class SwitchForm; }

class SwitchForm : public QWidget { Q_OBJECT

public: explicit SwitchForm(QWidget *parent = nullptr);

signals: void statusChanged(bool checked);

public slots: void updateValue();

private: void drawBackGround(QPainter *painter); void drawSlider(QPainter *painter);

protected: void paintEvent(QPaintEvent *ev); void mousePressEvent(QMouseEvent *ev);

private:

int m_space;                //滑块距离边界距离
   int m_radius;               //圆角角度

   bool m_checked;             //是否选中
   bool m_showText;            //是否显示文字
   bool m_showCircle;          //是否显示圆圈
   bool m_animation;           //是否使用动画

   QColor m_bgColorOn;         //打开时候的背景色
   QColor m_bgColorOff;        //关闭时候的背景色
   QColor m_sliderColorOn;     //打开时候滑块颜色
   QColor m_sliderColorOff;    //关闭时候滑块颜色
   QColor m_textColor;         //文字颜色

   QString m_textOn;           //打开时候的文字
   QString m_textOff;          //关闭时候的文字

   QTimer  *m_timer;            //动画定时器
   int     m_step;             //动画步长
   int     m_startX;           //滑块开始X轴坐标
   int     m_endX;             //滑块结束X轴坐标

public: int space() const; int radius() const; bool checked() const; bool showText() const; bool showCircel() const; bool animation() const;

QColor bgColorOn()          const;
QColor bgColorOff()         const;
QColor sliderColorOn()      const;
QColor sliderColorOff()     const;
QColor textColor()          const;

QString textOn()            const;
QString textOff()           const;

int step()                  const;
int startX()                const;
int endX()                  const;

public Q_SLOTS: void setSpace(int space); void setRadius(int radius); void setChecked(bool checked); void setShowText(bool show); void setShowCircle(bool show); void setAnimation(bool ok);

void setBgColorOn(const QColor &color);
void setBgColorOff(const QColor &color);
void setSliderColorOn(const QColor &color);
void setSliderColorOff(const QColor &color);
void setTextColor(const QColor &color);

void setTextOn(const QString &text);
void setTextOff(const QString &text);

};

bool getStatus();

#endif // SWITCHFORM_H

#include 'switchform.h' #include 'ui_switchform.h' #include

SwitchForm::SwitchForm(QWidget *parent) : QWidget(parent) { m_space = 2; m_radius = 5; m_checked = false; m_showText = true; m_showText = false; m_animation = true;

    m_bgColorOn = QColor(102, 205, 170);//设置按钮内部颜色
    m_bgColorOff = QColor(190, 190, 190);

    m_sliderColorOn = QColor(255, 255, 255);
    m_sliderColorOff = QColor(255, 255, 255);

    m_textColor = QColor(255, 255, 255);

    m_textOn = '开启';//设置按钮中的文字
    m_textOff = '关闭';

    m_step = 0;
    m_startX = 0;
    m_endX = 0;

    m_timer = new QTimer(this);
    m_timer->setInterval(30);
    connect(m_timer, SIGNAL(timeout()), this, SLOT(updateValue()));

}

// ... [代码省略] ...

Qt自定义控件:滑动开关按钮SwitchForm

原文地址: http://www.cveoy.top/t/topic/fASX 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录