当前位置:网站首页>Qt|模仿文字浮动字母
Qt|模仿文字浮动字母
2022-07-23 09:20:00 【InfoQ】
前沿

功能实现
定时器操作
virtual void timerEvent(QTimerEvent *event);
文本偏移实现
1:定义文本移动定时器
2:每一秒触发一次,每次触发都需要创建一个新的控件用于展示内容。
3:触发后将已经展示的文本遍历方式向上整体偏移,偏移过程中会根据随机数,随机更改文本字体的大小。
4:字体变化,导致文本的宽度高度变更,用新的宽度、高度计算偏移后的位置。
第一步
第二步
第三步
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);
第四步
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();
第五步
if (m_nTimerTriggerNumber == m_vetContent.size())
{
killTimer(m_nTimerId);
m_nTimerId = 0;
}
控件自绘
#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()
{
}
功能1:
QTime time = QTime::currentTime();
qsrand(time.msec() + time.second() * 1000);
int nFontSize = qrand() % 30 + 20;
QFont fontContent =this->font();
//设置:字体样式:微软雅黑
fontContent.setFamily("Microsoft YaHei");
//设置:字体大小:22
fontContent.setPixelSize(nFontSize);
//字体绑定
this->setFont(fontContent);
QFontMetrics metrics(font());
int nheight = metrics.height();
//设置内容并存储文本内容
m_qsTextContent = QString::fromLocal8Bit(sText.c_str());
setFixedHeight(nheight);
this->setText(m_qsTextContent);
this->adjustSize();
int nwidth = this->width();
//设置自定义QLable控件的宽度以及高度
QSize size(nwidth , nheight);
//开启定时器
m_nTimerId = startTimer(100);
return size;
功能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++)
{
//设置:色调(H)、饱和度(S)、亮度(Y)
int nIndex = (m_nStep + i) % 16;
color.setHsv((15 - nIndex) * 16, 255, 191);
painter->setPen(color);
//单个字符绘制
painter->drawText(x, y, QString(m_qsTextContent[i]));
//计算下一个字符的x坐标起始点
x += metrics.width(m_qsTextContent[i]);
}
总结
边栏推荐
- Dynamic programming -- knapsack problem
- Wacom firmware update error 123, digital board driver cannot be updated
- Aruba learning notes 05 configuration architecture WLAN configuration architecture
- Okrk3399 Development Board Reserved i2c4 Mounting EEPROM
- PKI体系快速介绍
- 利用shell脚本实现封禁扫描频率过高的ip
- 云呐|怎样管理固定资产?如何进行固定资产管理?
- 4. Find the median of two positive arrays
- 104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
- 【测试平台开发】十七、接口编辑页面实现下拉级联选择,绑定接口所属模块...
猜你喜欢

炫酷代码雨动态背景注册页面

Authing 支持 Zadig 啦!云原生用户统一认证快速对接

Using JS to parse and execute XSS automatically

Palindrome related topics

Can bus quick understanding

spotlight灯箱js插件全屏放大图片

【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
![Looking for peak [Abstract dichotomy exercise]](/img/99/122e79784f0f07120680d2cbcf89da.png)
Looking for peak [Abstract dichotomy exercise]

Right click to create a new TXT. The new text file is missing. You can solve it by adding a registry. Find the ultimate solution that can't be solved

JS texture style pie chart plug-in
随机推荐
CPU,内存,磁盘速度比较
CPU, memory, disk speed comparison
正则表达式常用语法解析
Uni app knowledge points and records of problems and solutions encountered in the project
The win11 installation system prompts that VirtualBox is incompatible and needs to uninstall the solution of virtual, but the uninstall list cannot find the solution of virtual
10年软件测试工程师经验,很茫然....
About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion
Canvas 从入门到劝朋友放弃(图解版)
利用shell脚本实现封禁扫描频率过高的ip
Which is a good fixed asset management system? What are the fixed asset management platforms?
JS software unloading prompt expression changes with the mouse JS special effect
First acquaintance and search set
ArcGIS uses DEM data to delineate the specific steps and processes of catchment area
【附下载】值得收藏的几款渗透测试常用的脚本
Palindrome related topics
基于EFR32MG24的AI 加速度姿势识别体验
ValidationError: Invalid options object. Dev Server has been initialized using an options object th
spotlight灯箱js插件全屏放大图片
Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?
After using vscode to format the code, save and find that the code is messy again. What should I do? Vs remove formatting