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

相关推荐
kuan_li_lyg8 分钟前
MATLAB - 小车倒立摆的非线性模型预测控制(NMPC)
开发语言·算法·matlab·机器人·mpc·模型预测控制·倒立摆
YKPG12 分钟前
c语言修炼秘籍 - - 禁(进)忌(阶)秘(技)术(巧)【第七式】程序的编译
linux·c语言·开发语言
灏瀚星空37 分钟前
画布交互系统深度优化:从动态缩放、小地图到拖拽同步的全链路实现方案
经验分享·笔记·python·microsoft·交互
lkbhua莱克瓦2441 分钟前
用c语言实现——一个带头节点的链队列,支持用户输入交互界面、初始化、入队、出队、查找、判空判满、显示队列、遍历计算长度等功能
c语言·数据结构·程序人生·算法·链表·交互·学习方法
TNTLWT1 小时前
程序生成随机数
开发语言
ansenXia1 小时前
CentOS系统中排查进程异常终止的日志
开发语言·python
ᖰ・◡・ᖳ2 小时前
前端之勇闯DOM关
开发语言·前端·javascript·学习·html
双叶8362 小时前
(51单片机)LCD显示温度(DS18B20教程)(LCD1602教程)(延时函数教程)(单总线教程)
c语言·开发语言·单片机·嵌入式硬件·mongodb·51单片机·nosql
好蛋编程2 小时前
【c语言】指针进阶
c语言·开发语言
二猛子2 小时前
Qt-托盘的实现
开发语言·qt