当前位置:网站首页>Qlabel text scrolling horizontally
Qlabel text scrolling horizontally
2022-06-25 23:35:00 【The struggle history of polar bear】
First on the renderings :
Look at the source code :
1. Adaptive function , Judge label Whether the text needs to scroll .
void MLabel::upateLabelRollingState()
{
// Get text size , Less than the length of the text box , No scrolling required
QFont ft = font();
ft.setPointSize(fontSize);
QFontMetrics fm(ft);
int nW = fm.width(text());
left = 0;
// Turn on text box scrolling
if(nW > width())
{
timerId = startTimer(100);
}
// Turn off text box scrolling
else
{
if(timerId >= 0)
{
killTimer(timerId);
timerId = -1;
}
}
}
2. Move the text position regularly
void MLabel::timerEvent(QTimerEvent *e)
{
if(e->timerId() == timerId && isVisible())
{
// Every time I move left 1 Pixel
left += 1;
// Determine whether the cycle has been completed , When finished, restore the starting position , Start the cycle again
QFont ft = font();
ft.setPointSize(fontSize);
QFontMetrics fm(ft);
int txtWidth = fm.width(text());
int spaceWidth = fm.width(strSpace);
if((txtWidth + spaceWidth) < left)
left = 0;
repaint();
}
QLabel::timerEvent(e);
}
3. Redraw events , Dynamically display text
void MLabel::paintEvent(QPaintEvent *e)
{
QPainter p(this);
// Get the size of the text box
QRect rc = rect();
rc.setHeight(rc.height() - 2);
rc.setWidth(rc.width() - 2);
// Set the font of the text to be drawn
QFont ft = font();
ft.setPointSize(fontSize);
p.setFont(ft);
p.setPen(QPen(Qt::red));
// Set the starting position of the drawn text , That is, how much to move the text to the left
rc.setLeft(rc.left() - left);
// If the text has been displayed to the end , Then add the text again , Make a circular scrolling effect
QString strText = text();
if(timerId >= 0)
strText += strSpace + text();
// Draw text
p.drawText(rc, Qt::AlignVCenter, strText);
}
4. Use effect , Setting text in 、 The scaling event calls the adaptive function twice .
void MLabel::setText(const QString & txt)
{
QLabel::setText(txt);
upateLabelRollingState();
}
void MLabel::resizeEvent(QResizeEvent *e)
{
QLabel::resizeEvent(e);
upateLabelRollingState();
}
complete , When using , take QLabel Upgrade to MLabel that will do .
Finally, the source download address is attached :https://download.csdn.net/download/chenxipu123/10966082
Make complaints about it :CSDN Do not set the download points required for resources , So the source code cannot be shared for free .
边栏推荐
- Sword finger offer 46 Translate numbers to strings (DP)
- Typora writing DOS commands
- How to use drawing comparison function in CAD
- cookie、session、token
- 我的vscode
- A. Balance the Bits--Codeforces Round #712 (Div. 1)
- YUV444、YUV422、YUV420、YUV420P、YUV420SP、YV12、YU12、NV12、NV21
- C. Yet Another Card Deck-Educational Codeforces Round 107 (Rated for Div. 2)
- 对卡巴斯基发现的一个将shellcode写入evenlog的植入物的复现
- cookie、session、token
猜你喜欢
RepOptimizer: 其实是RepVGG2
Use and difference between ue4\ue5 blueprint node delay and retroggable delay
Pycharm student's qualification expires, prompting no suitable licenses associated with account solution
CSDN添加页内跳转和页外指定段落跳转
Xampp重启后,MySQL服务就启动不了。
Idea auto generator generates constructor get/set methods, etc
After xampp restarts, the MySQL service cannot be started.
#23class介绍
做接口测试,这3种工具到底什么时候用?
转载: QTableWidget详解(样式、右键菜单、表头塌陷、多选等)
随机推荐
#24class静态成员
做接口测试,这3种工具到底什么时候用?
经典图像分割网络:Unet 支持libtorch部署推理【附代码】
Idea FAQ collection
Comp212 distributed protocol
UE4_ Ue5 combines the offline voice recognition plug-in for speech recognition
[opencv450 samples] create image list yaml
【AXI】解读AXI协议原子化访问
Leetcode (605) -- flower planting
Ad20 learning notes I
Why is the frame rate calculated by opencv wrong?
[opencv450 samples] inpaint restores the selected region in the image using the region neighborhood
Konva series tutorial 2: drawing graphics
golang Make a list of intervals with sequential numbers
Repoptimizer: it's actually repvgg2
[Axi] interpretation of Axi protocol disorder mechanism
自定义QComboBox下拉框,右对齐显示,下拉列表滑动操作
C. Fibonacci Words-April Fools Day Contest 2021
C language (I)
Day3 data types and operators summary and job