当前位置:网站首页>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]);
}
总结
边栏推荐
- Sword finger offer 46. translate numbers into strings
- 優化華為雲服務器采用Key登陸
- Optimisation du serveur Cloud Huawei avec connexion clé
- spotlight灯箱js插件全屏放大图片
- 关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
- First acquaintance and search set
- Qt文档阅读笔记-Audio Example解析
- 10 years of software testing engineer experience, very confused
- Introduction and mechanism of Aptos
- Is it risky and safe to open an account for stock speculation?
猜你喜欢

什么是Per-Title编码?

Aruba学习笔记05-配置架构- WLAN配置架构

对象使用过程中背后调用了哪些方法

【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID

LZ77 file compression

About flex layout justify content: the last solution to the misalignment of space around and why it is solved like this is a discussion

什麼是Per-Title編碼?
![[applet automation minium] i. framework introduction and environment construction](/img/1f/95b78e6574c3af3ff7abcf5db838f5.png)
[applet automation minium] i. framework introduction and environment construction

Which is a good fixed asset management system? What are the fixed asset management platforms?

Using JS to parse and execute XSS automatically
随机推荐
什么是Per-Title编码?
Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated
【小程序自动化Minium】一、框架介绍和环境搭建
[can I do your first project?] Detailed introduction and Simulation Implementation of gzip
C language implements memcpy and memmove
Canvas 从入门到劝朋友放弃(图解版)
JS calendar style pie chart statistics plug-in
云呐-如何加强固定资产管理?怎么加强固定资产管理?
第4章 集合运算
Official wechat product! Applet automation framework minium sharing Preview
VK36N5D抗电源干扰/手机干扰 5键5通道触摸检测芯片防呆功能触摸区域积水仍可操作
[pyqt5 installation and use]
Quick introduction to PKI system
Palindrome related topics
4. Find the median of two positive arrays
Surrounded Regions
Using shell script to block IP with high scanning frequency
FFmpeg 1 - 概览/安装
Is it risky and safe to open an account for stock speculation?
转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。