当前位置:网站首页>Qt writes custom controls: one of the text spotlight effects
Qt writes custom controls: one of the text spotlight effects
2022-08-05 07:27:00 【Nice ah, friend】
#ifndef WIDGET_H#define WIDGET_H#include #include class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();protected:void paintEvent(QPaintEvent *event)override;private:void onTimer();QString text;QTimer timer;QRect textRect;int changeValue{0};bool runDirectionIsRight{true};};#endif // WIDGET_H
#include "widget.h"#include #include #include Widget::Widget(QWidget *parent): QWidget(parent){text = "The water of the Yellow River comes from the sky and flows to the sea without returning";auto font = this->font();font.setPixelSize(40);font.setBold(true);setFont(font);connect(&timer,&QTimer::timeout,this,&Widget::onTimer);timer.start(40);}Widget::~Widget(){}void Widget::paintEvent(QPaintEvent *event){QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing,true);painter.setFont(this->font());auto rect = event->rect();painter.save();painter.setPen(QColor("#574E54"));painter.drawText(rect, Qt::AlignCenter | Qt::TextWordWrap, text);painter.restore();textRect = painter.boundingRect(rect,Qt::AlignCenter | Qt::TextWordWrap,text);auto pos = textRect.topLeft() + QPoint(changeValue,0);QPolygon polygon;polygon << pos << pos + QPoint(50,0);pos = textRect.bottomLeft() + QPoint(changeValue,0);polygon << pos + QPoint(25,0) << pos - QPoint(25,0);;QPainterPath path;path.addPolygon(polygon);painter.setClipPath(path);auto tempRect = polygon.boundingRect();QLinearGradient linearGradient(tempRect.topRight(),tempRect.bottomLeft());linearGradient.setColorAt(0.0,Qt::magenta);linearGradient.setColorAt(0.2,Qt::darkYellow);linearGradient.setColorAt(0.4,Qt::green);linearGradient.setColorAt(0.6,Qt::red);linearGradient.setColorAt(0.8,Qt::darkRed);linearGradient.setColorAt(1.0,Qt::blue);painter.setBrush(Qt::transparent);painter.setPen(QPen(QBrush(linearGradient),3));painter.drawText(rect, Qt::AlignCenter | Qt::TextWordWrap, text);}void Widget::onTimer(){if(runDirectionIsRight){changeValue += 15;if(changeValue >= textRect.width()){runDirectionIsRight = false;}}else{changeValue -= 15;if(changeValue <= 0){runDirectionIsRight = true;}}update();}
边栏推荐
- 谷歌零碎笔记之MVCC(草稿)
- 【LeetCode】235.二叉搜索树的最近公共祖先
- Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
- 访问被拒绝:“microsoft.web.ui.webcontrols”的解决办法
- 3555. 二叉树
- YOLOv3 SPP理论详解(包括CIoU及Focal loss)
- 2022.7.29好题选讲(计数专题)
- 专用机终端安装软件后报IP冲突
- 风控特征的优化分箱,看看这样教科书的操作
- MobileNetV1架构解析
猜你喜欢
php向mysql写入数据失败
2022 crane driver (limited bridge crane) exam question bank and simulation test
U++ UE4官方文档课后作业
风控特征的优化分箱,看看这样教科书的操作
AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
Use of thread pool (combined with Future/Callable)
400 times performance improvement 丨 swap valuation optimization case calculation
Redis 全套学习笔记.pdf,太全了
SVG大鱼吃小鱼动画js特效
IO process thread -> communication between processes -> day7
随机推荐
今天虚竹哥又发现了一款好用的国产化API工具
window.open 全屏展示
[instancetype type Objective-C]
爬虫之验证码
二叉搜索树问题
利用Jenkins的持续集成
【LeetCode】235.二叉搜索树的最近公共祖先
Antdesign a-select 下拉框超出长度换行显示
每一个女孩曾经都是一个没有泪的天使
爬虫从入门到入牢
DeFi 前景展望:概览主流 DeFi 协议二季度进展
Flink Learning 12: DataStreaming API
AI+视频技术助力保障校园安全,校园智能安防平台该如何建设?
TRACE32——加载符号表信息用于调试
MySQL: order by sorting query, group by grouping query
文本特征化方法总结
3555. 二叉树
Invalid operator for data type.The operator is add and the type is text.
protobuf is compiled against the associated .proto file
线程池的使用(结合Future/Callable使用)