[特殊字符]【Qt自定义控件】创意开关按钮 - 丝滑动画+自定义样式+信号交互 | 附完整源码

话不多说直接上代码

1、.mybutton.h

cpp 复制代码
#ifndef MYBUTTON_H
#define MYBUTTON_H

#include <QWidget>
#include <QPropertyAnimation>

class MyButton : public QWidget
{
    Q_OBJECT
public:
    explicit MyButton(QWidget *parent = nullptr);

protected:

    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void resizeEvent(QResizeEvent *event);

private:
    bool isOff =true;

    QBrush offBgBrush = Qt::black;
    QBrush offRBrush = Qt::red;

    //QBrush offBgBrush = Qt::gray;
    //QBrush offRBrush = Qt::black;

    QBrush onBgBrush = Qt::gray;
    QBrush onRBrush = Qt::green;


    //QString offText = "OFF";
    //QString onText = "ON";

   

    QString onText = QStringLiteral("是");
    QString offText = QStringLiteral("否");

    QPropertyAnimation *animation;
    int posX = height()/2;


signals:
    void isClick();
    void isClickedWithParams(bool);
};

#endif // MYBUTTON_H

2、mybutton.cpp

cpp 复制代码
#include "mybutton.h"

#include <QMouseEvent>
#include <QPainter>

MyButton::MyButton(QWidget *parent)
    : QWidget{parent}
{
    //setFixedSize(60,20);
    setFixedSize(240, 32);
    animation = new QPropertyAnimation(this);
    animation->setTargetObject(this);
    animation->setStartValue(height()/2);
    animation->setEndValue(width()-height()/2);
    animation->setEasingCurve(QEasingCurve::InCurve);
    animation->setDuration(500);
    connect(animation,&QPropertyAnimation::valueChanged,this,[=](const QVariant &value){
        posX = value.toInt();
        update();
    });
}
void MyButton::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    int radius = height()/2;
    painter.setPen(Qt::NoPen);
    painter.setBrush(isOff ? offBgBrush : onBgBrush);
    painter.drawRoundedRect(this->rect(),radius,radius);
    painter.setBrush(isOff ? offRBrush : onRBrush);
    QPoint center;
    center.setX(posX);
    center.setY(radius);
    painter.drawEllipse(center,radius-radius/10,radius-radius/10);
    //painter.drawEllipse(QPoint(radius,radius),radius*0.9,radius*0.9);
    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial",radius/2));
    painter.drawText(this->rect(),Qt::AlignCenter,isOff? offText:onText);

}
void MyButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        isOff ? animation->setDirection(QAbstractAnimation::Forward) : animation->setDirection(QAbstractAnimation::Backward);
        animation->start();
        emit isClick();
        isOff ? emit isClickedWithParams(true):emit isClickedWithParams(false);

        isOff = !isOff;
        //update();
    }
    // MyButton::mousePressEvent(event);
}

void MyButton::resizeEvent(QResizeEvent *event)
{
    animation->setStartValue(height()/2);

    animation->setEndValue(width()-height()/2);
}

3、效果展示

相关推荐
步、步、为营3 分钟前
.NET 事件模式举例介绍
java·开发语言·.net
~plus~6 分钟前
WPF八大法则:告别模态窗口卡顿
开发语言·经验分享·后端·程序人生·c#
march of Time16 分钟前
go工具库:hertz api框架 hertz client的使用
开发语言·golang·iphone
24K纯学渣38 分钟前
Python编码格式化之PEP8编码规范
开发语言·ide·python·pycharm
怒视天下40 分钟前
零基础玩转Python生物信息学:数据分析与算法实现
开发语言·python
GISer_Jing1 小时前
Three.js中AR实现详解并详细介绍基于图像标记模式AR生成的详细步骤
开发语言·javascript·ar
委婉待续1 小时前
Qt的学习(一)
开发语言·qt·学习
笨笨马甲1 小时前
Qt Quick Layout功能及架构
开发语言·qt
Dovis(誓平步青云)1 小时前
探索C++标准模板库(STL):String接口的底层实现(下篇)
开发语言·c++·stl·string
海棠一号2 小时前
JAVA理论第五章-JVM
java·开发语言·jvm