#ifndef SWITCHFORM_H
#define SWITCHFORM_H

#include <QWidget>
#include <QTimer>
#include <QColor>

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) override;
    void mousePressEvent(QMouseEvent *ev) override;

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 isChecked()              const;
    bool showText()             const;
    bool showCircle()           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);

public:
    bool getStatus() const; // 添加一个槽函数,表示当前开关状态
};

#endif // SWITCHFORM_H
#include 'switchform.h'
#include 'ui_switchform.h'
#include <QPainter>

SwitchForm::SwitchForm(QWidget *parent) :
    QWidget(parent)
{
    m_space = 2;
    m_radius = 5;
    m_checked = false;
    m_showText = true;
    m_showCircle = 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, &QTimer::timeout, this, &SwitchForm::updateValue);
}

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

bool SwitchForm::getStatus() const
{
    return m_checked;
}

这段代码实现了一个自定义的Qt滑动开关控件,具有以下特点:

  • 可自定义外观: 可以设置开关的颜色、圆角、文字等属性。
  • 支持动画效果: 开关状态切换时可以显示平滑的动画效果。
  • 使用方便: 提供了简单易用的接口来设置和获取开关状态。

你可以根据需要修改代码,例如添加新的功能或修改现有功能的行为。

自定义Qt滑动开关控件(Switch)

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

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