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

[特殊字符]【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、效果展示
在这里插入图片描述

http://www.xdnf.cn/news/1109.html

相关文章:

  • 人工智能在PET-CT中的应用方向探析
  • 环境变量(Linux)
  • SparkStreaming概述
  • C# MP3 伴奏
  • MYSQL之库的操作
  • C++设计模式
  • Sharding-JDBC 系列专题 - 第七篇:Spring Boot 集成与 Sharding-Proxy 简介
  • MongoDB常用命令
  • 01-STM32基本知识点和keil5的安装
  • 【白雪讲堂】[特殊字符]内容战略地图|GEO优化框架下的内容全景布局
  • 7N60-ASEMI无人机专用功率器件7N60
  • Elasticsearch 使用reindex进行数据同步或索引重构
  • spark jar依赖顺序
  • Docker--Docker网络原理
  • docker容器中uv的使用
  • ARM汇编的LDM和STM指令
  • 【sylar-webserver】重构 增加内存池
  • weibo_har鸿蒙微博分享,单例二次封装,鸿蒙微博,微博登录
  • 基于 WebRTC + Pion + HeyGem.ai 实现可互动的数字人系统
  • 安徽京准:关于石油管道监控NTP时间同步服务器方案
  • kafka auto.offset.reset详解
  • 【Qt】QDialog类
  • WebRTC服务器Coturn服务器的管理平台功能
  • 最新扣子(Coze)案例教程:飞书多维表格按条件筛选记录 + 读取分页Coze工作流,无限循环使用方法,手把手教学,完全免费教程
  • 【基于SprintBoot+Mybatis+Mysql】电脑商城项目之显示勾选的购物车数据和创建订单
  • 电气动调节单座V型球阀带阀杆节流套沟槽孔板的作用-耀圣
  • 【华为HCIP | 华为数通工程师】821—多选解析—第十二页
  • 【MySQL】MySQL 表的增删改查(CRUD)—— 下篇(内含聚合查询、group by和having子句、联合查询、插入查询结果)
  • Linux信号的产生
  • Qt绘制可选择范围的日历