当前位置: 首页 > news >正文

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

话不多说直接上代码
1、.mybutton.h

#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

#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、效果展示
在这里插入图片描述

相关文章:

  • OpenLDAP 管理 ELK 用户
  • PHP通讯录网站源码无需sql数据库
  • 【中级软件设计师】程序设计语言基础成分
  • 从零开始创建MCP Server实战指南
  • STM32外部中断与外设中断区别
  • Element Plus表格组件深度解析:构建高性能企业级数据视图
  • Vue2-指令语法
  • C++静态与动态联编区别解析
  • Windows安装Hadoop(图文解说版)
  • 【华为HCIP | 华为数通工程师】821—多选解析—第十二页
  • Spring中配置 Bean 的两种方式:XML 配置 和 Java 配置类
  • NDSS 2025|侧信道与可信计算攻击技术导读(二)系统化评估新旧缓存侧信道攻击技术
  • Mininet--node.py源码解析
  • ViViT: 一种视频视觉Transformer
  • Cline 之Plan和Act模式
  • [大模型]AI Agent入门01——AI Agent概念梳理
  • Ollama 实战手册
  • 植被参数遥感反演技术革命!AI+Python支持向量机/随机森林/神经网络/CNN/LSTM/迁移学习在植被参数反演中的实战应用与优化
  • spark jar依赖顺序
  • SpringBoot原生实现分布式MapReduce计算
  • 宁夏中卫深化公立医院机构编制改革:市人民医院机构规格升为正处级
  • 第四届全民阅读大会·阅读权益保障论坛举办
  • 【社论】上海车展40年,见证了什么
  • 哈佛大学就联邦经费遭冻结起诉特朗普政府
  • 人民日报聚焦外贸“重镇”福建晋江:多元化布局扩大“朋友圈”
  • 空山日落雨初收,来文徵明的画中听泉