当前位置:网站首页>Qt编写自定义控件:文字聚光灯效果之一
Qt编写自定义控件:文字聚光灯效果之一
2022-08-05 07:23:00 【友善啊,朋友】
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
class Widget : public QWidget
{
Q_OBJECT
public:
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 <QPainter>
#include <QPaintEvent>
#include <QPainterPath>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
text = "黄河之水天上来,奔流到海不复回";
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();
}
边栏推荐
猜你喜欢
强网杯2022 pwn 赛题解析——house_of_cat
MySQL:连接查询 | 内连接,外连接
七夕?编程?
busybox 知:构建
本地能ping通虚拟机,虚拟机ping不通本地
Vulnhub target drone: HA_ NARAK
Modeling of the MAYA ship
UDP broadcast
AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
随机推荐
图片地址转为base64
2022.8.2 模拟赛
How to avoid online memory leaks
Unity—物理引擎+“武器模块”
Flink Learning 11: Flink Program Parallelism
性能提升400倍丨外汇掉期估值计算优化案例
MySQL:order by排序查询,group by分组查询
爬虫之验证码
标准C语言15
Shiny04---Application of DT and progress bar in shiny
IO process thread -> communication between processes -> day7
JS实现从照片中裁切自已的肖像
2022.7.29好题选讲(计数专题)
Libpq 是否支持读写分离配置
Mysql主从延迟的原因和解决方案
Flink Learning 10: Use idea to write WordCount and package and run
C# FileSystemWatcher
RK3568环境安装
TCP sticky packet unpacking problem + solution
MAYA船的建模