当前位置:网站首页>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();
}
边栏推荐
- 二叉树进阶复习1
- 线性代数对角化
- 线程池的使用(结合Future/Callable使用)
- Redis 全套学习笔记.pdf,太全了
- 双向循环带头链表
- Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions
- IO process thread -> communication between processes -> day7
- C# FileSystemWatcher
- Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
- 《基于R语言的自动数据收集》--第3章 XML和JSON
猜你喜欢

UDP broadcast

Redis数据库学习

Modeling of the MAYA ship

Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation

RNote108---显示R程序的运行进度

TRACE32——通用寄存器查看与修改

TRACE32——Break

In the anaconda Promat interface, import torch is passed, and the error is reported in the jupyter notebook (only provide ideas and understanding!)

Algorithm Supplements Fifteen Complementary Linked List Related Interview Questions

Shiny04---Application of DT and progress bar in shiny
随机推荐
Task flow scheduling tool AirFlow,, 220804,,
关于MP3文件中找不到TAG标签的问题
TRACE32——Break
向美国人学习“如何快乐”
Advanced Redis
C# FileSystemWatcher
Access Denied: "microsoft.web.ui.webcontrols" workaround
(4) Rotating object detection data roLabelImg to DOTA format
Unity—物理引擎+“武器模块”
RNote108---显示R程序的运行进度
Tencent Business Security Post IDP Talk Summary
Flink Learning 12: DataStreaming API
MongoDB 语法大全
MySQL:连接查询 | 内连接,外连接
FPGA parsing B code----serial 4
Liunx教程超详细(完整)
cmake 学习使用笔记(三)
An IP conflict is reported after installing the software on a dedicated computer terminal
【 LeetCode 】 235. A binary search tree in recent common ancestor
Week 8 Document Clustering(文本聚类)