当前位置:网站首页>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();
}
边栏推荐
- 2006年星座运势全解-射手
- AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
- Flink学习10:使用idea编写WordCount,并打包运行
- C# FileSystemWatcher
- 工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯
- Mysql为什么 建立数据库失败
- 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
- FPGA parsing B code----serial 4
- Does Libpq support read-write separation configuration?
- protobuf根据有关联的.proto文件进行编译
猜你喜欢
随机推荐
文本特征化方法总结
VXE-Table融合多语言
MySQL: JDBC programming
693. 行程排序
唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
It turns out that Maya Arnold can also render high-quality works!Awesome Tips
Tencent Internship Summary
基于 Docker 快速使用远程(云)数据库
mysql使用in函数的一个小问题
[instancetype type Objective-C]
Tencent Business Security Post IDP Talk Summary
2006年星座运势全解-巨蟹
Flink Learning 12: DataStreaming API
性能提升400倍丨外汇掉期估值计算优化案例
国家强制性灯具安全标准GB7000.1-2015
[上海]招聘.Net高级软件工程师&BI数据仓库工程师(急)
游戏思考19:游戏多维计算相关:点乘、叉乘、点线面距离计算
Redis常用命令
openSource 知:社区贡献
不能比较或排序 text、ntext 和 image 数据类型









