当前位置:网站首页>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();
}
边栏推荐
- Put Cloudflare on the website (take Tencent Cloud as an example)
- MAYA大炮建模
- Tencent Business Security Post IDP Talk Summary
- MySQL: JDBC programming
- 对数据类型而言运算符无效。运算符为 add,类型为 text。
- DeFi 前景展望:概览主流 DeFi 协议二季度进展
- 微信 小程序 之PC端 不支持 wx.previewMedia 方法 故用自定义轮播图进行 模拟照片视频的播放
- AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
- 环网冗余式CAN/光纤转换器 CAN总线转光纤转换器中继集线器hub光端机
- PCI Pharma Services Announces Multi-Million Dollar Expansion of UK Manufacturing Facility to Meet Growing Demand for Global High Potency Drug Manufacturing Services to Support Oncology Treatment
猜你喜欢
MobileNetV1架构解析
Vulnhub target drone: HA_ NARAK
唤醒手腕 - 微信小程序、QQ小程序、抖音小程序学习笔记(更新中)
TRACE32——外设寄存器查看与修改
今天虚竹哥又发现了一款好用的国产化API工具
Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
每月稳定干2万
Shiny04---Application of DT and progress bar in shiny
re正则表达式
cmake 学习使用笔记(三)
随机推荐
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
二叉树进阶复习1
window.open 全屏展示
691. 立方体IV
Shiny04---Application of DT and progress bar in shiny
MySQL: join query | inner join, outer join
PCI Pharma Services Announces Multi-Million Dollar Expansion of UK Manufacturing Facility to Meet Growing Demand for Global High Potency Drug Manufacturing Services to Support Oncology Treatment
Flink学习12:DataStreaming API
RNote108---显示R程序的运行进度
性能提升400倍丨外汇掉期估值计算优化案例
MySQL:order by排序查询,group by分组查询
[Tool Configuration] Summary of Common Uses of VSCode
GAN生成动漫头像Pytorch
【instancetype类型 Objective-C】
2022.8.2 模拟赛
Invalid operator for data type.The operator is add and the type is text.
mysql使用in函数的一个小问题
Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
Access Denied: "microsoft.web.ui.webcontrols" workaround
二叉搜索树问题