[特殊字符]【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、效果展示

相关推荐
苦夏木禾6 分钟前
js请求避免缓存的三种方式
开发语言·javascript·缓存
超级土豆粉14 分钟前
Turndown.js: 优雅地将 HTML 转换为 Markdown
开发语言·javascript·html
小彭努力中1 小时前
141.在 Vue 3 中使用 OpenLayers Link 交互:把地图中心点 / 缩放级别 / 旋转角度实时写进 URL,并同步解析显示
前端·javascript·vue.js·交互
wei_shuo1 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai
熊猫钓鱼>_>1 小时前
用Python解锁图像处理之力:从基础到智能应用的深度探索
开发语言·图像处理·python
GO兔1 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
好开心啊没烦恼2 小时前
Python 数据分析:numpy,抽提,整数数组索引与基本索引扩展(元组传参)。听故事学知识点怎么这么容易?
开发语言·人工智能·python·数据挖掘·数据分析·numpy·pandas
梓贤Vigo3 小时前
【Axure高保真原型】动态打字输入效果
交互·产品经理·axure·原型
future14123 小时前
C#学习日记
开发语言·学习·c#
king_harry3 小时前
Java程序-OceanBase Connector/J 示例
开发语言