《QT实用小工具·五十一》带动画的 CheckBox

2024-05-03 04:04

本文主要是介绍《QT实用小工具·五十一》带动画的 CheckBox,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、概述
源码放在文章末尾

该项目实现了带动画效果的多选框,鼠标放在上面或者选中都会呈现炫酷的动画效果,demo演示如下:

在这里插入图片描述

项目部分代码如下所示:


#ifndef LINEARCHECKBOX_H
#define LINEARCHECKBOX_H#include <QCheckBox>
#include <QPropertyAnimation>
#include <QPainter>
#include <QPainterPath>
#include <QDebug>class AniCheckBox : public QCheckBox
{Q_OBJECTQ_PROPERTY(double hover_prog READ getHoverProg WRITE setHoverProg)Q_PROPERTY(double part_prog READ getPartProg WRITE setPartProg)Q_PROPERTY(double check_prog READ getCheckProg WRITE setCheckProg)
public:AniCheckBox(QWidget* parent = nullptr);void setForeColor(QColor c);protected:void paintEvent(QPaintEvent *) override;void enterEvent(QEvent *e) override;void leaveEvent(QEvent *e) override;bool hitButton(const QPoint &) const override;virtual void checkStateChanged(int state);virtual void drawBox(QPainter &painter, QRectF rect);QPropertyAnimation* startAnimation(const QByteArray &property, double begin, double end, int duration = 500, QEasingCurve curve = QEasingCurve::OutQuad);protected:double getHoverProg() const;void setHoverProg(double prog);double getPartProg() const;void setPartProg(double prog);double getCheckProg() const;void setCheckProg(double prog);protected:int boxSide = 0; // 选择框边长,0为自适应QColor foreColor = QColor("#2753ff"); // 前景颜色double hoverProg = 0;   // 鼠标移上去的进度double partyProg = 0;   // 部分选中的进度double checkProg = 0;   // 选中的进度
};#endif // LINEARCHECKBOX_H

#include "anicheckbox.h"AniCheckBox::AniCheckBox(QWidget *parent) : QCheckBox(parent)
{setCursor(Qt::PointingHandCursor);connect(this, &QCheckBox::stateChanged, this, [=](int state) {// qInfo() << "状态变化:" << static_cast<Qt::CheckState>(state);checkStateChanged(state);});
}void AniCheckBox::setForeColor(QColor c)
{this->foreColor = c;
}void AniCheckBox::paintEvent(QPaintEvent *)
{// QCheckBox::paintEvent(e);QPainter painter(this);// painter.setRenderHint(QPainter::Antialiasing, true);QRectF rect;double textLeft;if (boxSide <= 0){// 自适应大小:优先一行文字大小,其次按比例const double fixedProp = 0.8; // 默认比例QFontMetricsF fm(painter.font());double side = fm.height(); // 一行文字的高度if (side >= this->height() * fixedProp)side = this->height() * fixedProp;double margin = side / 2;rect = QRectF(margin, (height() - side) / 2, side, side);textLeft = rect.right() + margin;}else{// 固定大小double margin = (this->height() - boxSide) / 2;rect = QRectF(margin, margin, boxSide, boxSide);textLeft = rect.right() + margin;}// 绘制选择框painter.save();drawBox(painter, rect);painter.restore();// 绘制文字painter.save();painter.drawText(QRectF(textLeft, 0, this->width() - textLeft, this->height()), this->text(), Qt::AlignVCenter | Qt::AlignLeft);painter.restore();
}void AniCheckBox::enterEvent(QEvent *e)
{QCheckBox::enterEvent(e);startAnimation("hover_prog", getHoverProg(), 1);
}void AniCheckBox::leaveEvent(QEvent *e)
{QCheckBox::leaveEvent(e);startAnimation("hover_prog", getHoverProg(), 0);
}bool AniCheckBox::hitButton(const QPoint &) const
{return true;
}void AniCheckBox::checkStateChanged(int state)
{if (state == Qt::Unchecked){startAnimation("check_prog", getCheckProg(), 0, 800, QEasingCurve::OutBounce);}else if (state == Qt::PartiallyChecked){}else if (state == Qt::Checked){startAnimation("check_prog", getCheckProg(), 1, 500, QEasingCurve::OutBack);}
}void AniCheckBox::drawBox(QPainter& painter, QRectF rect)
{painter.setPen(foreColor);painter.setRenderHint(QPainter::Antialiasing, true);// 绘制边缘方框,和悬浮状态有关double radius = 3;radius *= (1 - hoverProg);painter.drawRoundedRect(rect, radius, radius);// 绘制选中状态int state = this->checkState();double prop = 0.6;prop *= checkProg;rect = QRectF(rect.left() + rect.width() * (1 - prop) / 2,rect.top() + rect.height() * (1 - prop) / 2,rect.width() * prop,rect.height() * prop);QPainterPath path;path.addRoundedRect(rect, radius, radius);painter.fillPath(path, foreColor);if (state == Qt::Unchecked){}else if (state == Qt::PartiallyChecked){}else if (state == Qt::Checked){}
}QPropertyAnimation *AniCheckBox::startAnimation(const QByteArray &property, double begin, double end, int duration, QEasingCurve curve)
{QPropertyAnimation* ani = new QPropertyAnimation(this, property);ani->setStartValue(begin);ani->setEndValue(end);ani->setDuration(duration);ani->setEasingCurve(curve);connect(ani, SIGNAL(finished()), ani, SLOT(deleteLater()));connect(ani, SIGNAL(valueChanged(const QVariant&)), this, SLOT(update()));ani->start();return ani;
}double AniCheckBox::getHoverProg() const
{return hoverProg;
}void AniCheckBox::setHoverProg(double prog)
{this->hoverProg = prog;
}double AniCheckBox::getPartProg() const
{return partyProg;
}void AniCheckBox::setPartProg(double prog)
{this->partyProg = prog;
}double AniCheckBox::getCheckProg() const
{return checkProg;
}void AniCheckBox::setCheckProg(double prog)
{this->checkProg = prog;
}

源码下载

这篇关于《QT实用小工具·五十一》带动画的 CheckBox的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/955796

相关文章

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可

Linux系统之stress-ng测压工具的使用

《Linux系统之stress-ng测压工具的使用》:本文主要介绍Linux系统之stress-ng测压工具的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、理论1.stress工具简介与安装2.语法及参数3.具体安装二、实验1.运行8 cpu, 4 fo