当前位置:网站首页>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();} 
边栏推荐
- Modeling of the MAYA ship
- 栈与队列的基本介绍和创建、销毁、出入、计算元素数量、查看元素等功能的c语言实现,以及栈的压入、弹出序列判断,栈结构的链式表示与实现
- TRACE32——SMP多核调试
- 唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
- 谷歌零碎笔记之MVCC(草稿)
- MySQL:order by排序查询,group by分组查询
- 2022 crane driver (limited bridge crane) exam question bank and simulation test
- Shiny04---DT和进度条在shiny中的应用
- RK3568 environment installation
- MobileNetV1架构解析
猜你喜欢

re正则表达式

Vulnhub target drone: HA_ NARAK

cmake 学习使用笔记(三)

Hash 这些知识你也应该知道

2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams

高端无主灯设计灯光设计该如何布置射灯灯具?

TRACE32——List源代码查看

餐饮大单品「真香」,却没有穿透周期的能力

Flink学习11:flink程序并行度

Falsely bamboo brother today and found a localization of API to use tools
随机推荐
Flink Learning 12: DataStreaming API
2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams
常用的遍历map的方法
SVG大鱼吃小鱼动画js特效
v-if/v-else根据计算判断是否显示
Shiny02---Shiny exception solution
图片地址转为base64
利用Jenkins的持续集成
Vulnhub靶机:HA_ NARAK
今天虚竹哥又发现了一款好用的国产化API工具
Redis数据库学习
Hash these knowledge you should also know
TRACE32——Break
Liunx教程超详细(完整)
配合屏幕录像专家,又小又清晰!
Week 8 Document Clustering
Put Cloudflare on the website (take Tencent Cloud as an example)
moment的使用
Flink学习10:使用idea编写WordCount,并打包运行
MySQL: join query | inner join, outer join