Qt自定义控件:实现炫酷开关按钮

本文将介绍如何使用Qt框架自定义一个美观且功能强大的开关按钮控件。该控件将具有以下特点:

  • 自定义外观: 可以自定义按钮的颜色、圆角、文字等属性。- 动画效果: 开关状态切换时带有平滑的动画过渡效果。- 状态判断: 提供函数方便地判断当前开关是开启还是关闭状态。

一、控件实现

1. 头文件 (switchform.h)cpp#ifndef SWITCHFORM_H#define SWITCHFORM_H

#include #include

class SwitchForm : public QWidget{ Q_OBJECT

public: explicit SwitchForm(QWidget *parent = nullptr); bool getStatus(int number); // 判断开关状态

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: // ... getter 和 setter 函数 ...

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);};

#endif // SWITCHFORM_H

2. 源文件 (switchform.cpp)cpp#include 'switchform.h'#include #include #include

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(10);     connect(m_timer, SIGNAL(timeout()), this, SLOT(updateValue()));}

// ... 其他函数 ...

void SwitchForm::drawBackGround(QPainter *painter){ // ... 绘制背景 ...}

void SwitchForm::drawSlider(QPainter *painter){ // ... 绘制滑块 ...}

void SwitchForm::paintEvent(QPaintEvent *ev){ // ... 绘制逻辑 ...}

void SwitchForm::mousePressEvent(QMouseEvent *ev){ // ... 处理鼠标点击事件 ...}

void SwitchForm::updateValue(){ // ... 更新动画 ...}

// ... getter 和 setter 函数 ...

bool SwitchForm::getStatus(int number){ // 判断奇偶数来确定开关状态 return (number % 2) != 0;

Qt自定义控件实现炫酷开关按钮

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

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