当前位置:网站首页>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();} 
边栏推荐
- Redis 全套学习笔记.pdf,太全了
- 小本创业者的致胜法宝!
- 线程池的创建及参数设置详解
- 导出SQLServer数据到Excel中
- Flink Learning 10: Use idea to write WordCount and package and run
- MobileNetV1架构解析
- Win10 设置锁屏壁纸提示尝试其它图片
- MySQL: order by sorting query, group by grouping query
- Summary of Text Characterization Methods
- Access Denied: "microsoft.web.ui.webcontrols" workaround
猜你喜欢

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

Vulnhub靶机:HA_ NARAK

Redis 全套学习笔记.pdf,太全了

Flink Learning 11: Flink Program Parallelism

After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career

MAYA船的建模

今天虚竹哥又发现了一款好用的国产化API工具

MySQL: join query | inner join, outer join

关于MP3文件中找不到TAG标签的问题

Flink Learning 12: DataStreaming API
随机推荐
Flink学习11:flink程序并行度
线程池的使用(结合Future/Callable使用)
Illegal key size 报错问题
AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
Put Cloudflare on the website (take Tencent Cloud as an example)
Shiny04---DT和进度条在shiny中的应用
cmake 学习使用笔记(三)
Redis数据库学习
国家强制性灯具安全标准GB7000.1-2015
二叉搜索树问题
外企Office常用英语
Does Libpq support read-write separation configuration?
常用的遍历map的方法
busybox 知:构建
Modeling of the MAYA ship
唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
Invalid operator for data type.The operator is add and the type is text.
Tencent Business Security Post IDP Talk Summary
Shiny04---Application of DT and progress bar in shiny
Bluetooth gap protocol