当前位置:网站首页>Qt| imitation text floating letter
Qt| imitation text floating letter
2022-07-23 14:49:00 【InfoQ】
the front

Function realization
Timer operation
virtual void timerEvent(QTimerEvent *event);
Text offset implementation
1: Define the text movement timer
2: Trigger every second , Each trigger requires the creation of a new control to display the content .
3: After triggering, the displayed text traversal mode will be shifted upward as a whole , Random numbers will be used in the migration process , Randomly change the size of the text font .
4: Font changes , Cause the width and height of the text to change , With a new width 、 Position after height calculation offset .
First step
The second step
The third step
QCustomLabel *lab = new QCustomLabel(this);
QSize size = lab->SetTextData(m_vetContent[m_nTimerTriggerNumber], 1);
int nTop = (height() - size.height()) / 2;
lab->setGeometry(DistanceLeft, nTop, size.width(), size.height());
lab->show();
m_vetControls.push_font(lab);
Step four
for (int i = 0; i < m_vetControls.size(); i++)
{
QCustomLabel *lab = m_vetControls[i];
QSize size = lab->UpdateZoomStyle(bFontZoom);
QRectF rectF = lab->geometry();
if (nTop == 0)
{
nTop = lab->geometry().top();
}
else
{
nTop = nTop - size.height();
}
lab->move(DistanceLeft, nTop);
}
this->update();
Step five
if (m_nTimerTriggerNumber == m_vetContent.size())
{
killTimer(m_nTimerId);
m_nTimerId = 0;
}
Control self drawing
#pragma once
#include <QLabel>
class QCustomLabel : public QLabel
{
Q_OBJECT
public:
QCustomLabel(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
~QCustomLabel();
};
#include "QCustomLabel.h"
QCustomLabel::QCustomLabel(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
{
}
QCustomLabel::~QCustomLabel()
{
}
function 1:
QTime time = QTime::currentTime();
qsrand(time.msec() + time.second() * 1000);
int nFontSize = qrand() % 30 + 20;
QFont fontContent =this->font();
// Set up : Font style : Microsoft YaHei
fontContent.setFamily("Microsoft YaHei");
// Set up : font size :22
fontContent.setPixelSize(nFontSize);
// Font binding
this->setFont(fontContent);
QFontMetrics metrics(font());
int nheight = metrics.height();
// Set content and store text content
m_qsTextContent = QString::fromLocal8Bit(sText.c_str());
setFixedHeight(nheight);
this->setText(m_qsTextContent);
this->adjustSize();
int nwidth = this->width();
// Set customization QLable The width and height of the control
QSize size(nwidth , nheight);
// Turn on timer
m_nTimerId = startTimer(100);
return size;
function 2:
QFontMetrics metrics(font());
int x = 0;
int y = (height() + metrics.ascent() - metrics.descent()) / 2;
QColor color;
for (int i = 0; i < m_qsTextContent.size(); i++)
{
// Set up : tonal (H)、 saturation (S)、 brightness (Y)
int nIndex = (m_nStep + i) % 16;
color.setHsv((15 - nIndex) * 16, 255, 191);
painter->setPen(color);
// Single character drawing
painter->drawText(x, y, QString(m_qsTextContent[i]));
// Calculate the of the next character x Coordinate starting point
x += metrics.width(m_qsTextContent[i]);
}
summary
边栏推荐
猜你喜欢

什麼是Per-Title編碼?

JS software unloading prompt expression changes with the mouse JS special effect

10 years of software testing engineer experience, very confused

【附下载】值得收藏的几款渗透测试常用的脚本

云呐|公司固定资产如何管理?公司固定资产如何管理比较好?

Spotlight light box JS plug-in full screen enlarged picture

【面试高频】cookie、session、token?看完再也不担心被问了
![Looking for peak [Abstract dichotomy exercise]](/img/99/122e79784f0f07120680d2cbcf89da.png)
Looking for peak [Abstract dichotomy exercise]

【小程序自动化Minium】一、框架介绍和环境搭建

【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
随机推荐
【测试平台开发】二十、完成编辑页发送接口请求功能
【软件测试】盘一盘工作中遇到的 MQ 异常测试
Quanzhi f1c100s/f1c200s learning notes (13) -- lvgl transplantation
Palindrome related topics
Due to resource constraints, the namenode fails to start with an error unable to create new native thread
【数组&&字符串&&宏练习题】
OpenHarmony南向学习笔记——Hi3861+HC-SR04超声波检测
Work notes: one time bag grabbing
Transferred from Yuxi information disclosure: products such as mRNA covid-19 vaccine and Jiuzhou horse tetanus immunoglobulin are expected to be on the market within this year.
【刷题记录】19. 删除链表的倒数第 N 个结点
CSDN写文方法(二)
R语言实战应用案例:绘图篇(三)-多种组合图型绘制
基本51单片机点阵汉字显示程序设计
cmake笔记
第三章 复杂一点的查询
Quick introduction to PKI system
Aruba learning notes 05 configuration architecture WLAN configuration architecture
全志F1C100S/F1C200S学习笔记(13)——LVGL移植
10 years of software testing engineer experience, very confused
[download attached] several scripts commonly used in penetration testing that are worth collecting