公司网站的ftp是什么,做网站图片为什么不清晰,自己可以做类似淘宝客网站吗,wordpress 页面空白页Qt窗口动画实战#xff1a;Qt实现呼吸灯效果
在嵌入式设备或桌面应用中#xff0c;呼吸灯效果是一种常见且优雅的UI动画#xff0c;常用于指示系统状态或吸引用户注意。本文将介绍如何使用Qt动画框架实现平滑的呼吸灯效果。
一、实现原理
利用Qt自带的动画框架来实现Qt实现呼吸灯效果
在嵌入式设备或桌面应用中呼吸灯效果是一种常见且优雅的UI动画常用于指示系统状态或吸引用户注意。本文将介绍如何使用Qt动画框架实现平滑的呼吸灯效果。
一、实现原理
利用Qt自带的动画框架来实现具体实现看代码
2、代码实现
#ifndef BUTTON_H
#define BUTTON_H#include QPropertyAnimation
#include QSequentialAnimationGroup
#include QPainter
#include QColor
#include QWidgetclass BreathingLight : public QWidget {Q_OBJECTQ_PROPERTY(int alpha READ alpha WRITE setAlpha)public:BreathingLight(QWidget *parent nullptr) : QWidget(parent), m_alpha(0) {setFixedSize(200, 200);// 创建两个动画一个从0到255一个从255到0QPropertyAnimation *animationUp new QPropertyAnimation(this, alpha);animationUp-setDuration(2500); // 动画时长为2000毫秒animationUp-setStartValue(20); // 起始透明度animationUp-setEndValue(255); // 结束透明度animationUp-setEasingCurve(QEasingCurve::InOutQuad); // 使用平滑的缓入缓出动画曲线QPropertyAnimation *animationDown new QPropertyAnimation(this, alpha);animationDown-setDuration(2500); // 动画时长为2000毫秒animationDown-setStartValue(255); // 起始透明度animationDown-setEndValue(20); // 结束透明度animationDown-setEasingCurve(QEasingCurve::InOutQuad); // 使用平滑的缓入缓出动画曲线// 创建一个动画组将两个动画添加进去并设置为循环播放QSequentialAnimationGroup *animationGroup new QSequentialAnimationGroup(this);animationGroup-addAnimation(animationUp);animationGroup-addAnimation(animationDown);animationGroup-setLoopCount(-1); // 无限循环animationGroup-start(); // 启动动画组}int alpha() const { return m_alpha; }void setAlpha(int alpha) {m_alpha alpha;update(); // 更新窗口触发重绘事件}protected:void paintEvent(QPaintEvent *event) override {Q_UNUSED(event);QPainter painter(this);QColor color(0, 255, 0, m_alpha); // 绿色使用 m_alpha 透明度painter.setBrush(color);painter.setPen(Qt::NoPen);QRect paint_rect rect();paint_rect.adjust(90, 90, -90, -90);painter.drawEllipse(paint_rect); // 绘制一个椭圆填充整个窗口}private:int m_alpha;
};#include QApplication
#include QTableView
#include QHeaderView
#include QStandardItemModel
#include button.hint main(int argc, char *argv[])
{QApplication a(argc, argv);//button.show();BreathingLight light;light.show();return a.exec();
}#endif // BUTTON_H3、总结
利用Qt自带的动画系统可以很方便的就做出炫酷的效果相比较其他传统的UIQt这个方案对用户来说其实还是很方便的。