当前位置:网站首页>Qlabel marquee text display
Qlabel marquee text display
2022-07-06 18:52:00 【HL_ Aeolus】
Effect display :
The whole function is optimized and modified on the basis of predecessors
One important point is :
When windows The system sets the text display scale to be not 100% When , Setting the font size number here does not mean that the pixel size of the text is this number
The point is this 3 A function :
// Adaptive function , Judge label Whether the text needs to scroll
void upateLabelRollingState();
// Format text font
void setScrollLabelFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
// Force the running light on , All text scrolls
void setFixedScrollShow(bool isOn);
Source program submission
The header file :
#ifndef SCROLL_LABEL_H
#define SCROLL_LABEL_H
#include <QDebug>
#include <QLabel>
#include <QTimerEvent>
#include <QPaintEvent>
#include <QTextDocument> // For judging rich text
#include <QPainter>
// Inherited from tag , After the qt Right click inside the designer to select " promote " Make this inherited class control the corresponding labels that need to be scrolled
class ScrollLabel :public QLabel
{
Q_OBJECT
public:
explicit ScrollLabel(QWidget *parent = nullptr);
~ScrollLabel();
// Adaptive function , Judge label Whether the text needs to scroll
void upateLabelRollingState();
// Format text font
void setScrollLabelFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
// Force the running light on , All text scrolls
void setFixedScrollShow(bool isOn);
public slots:
// Change the displacement regularly , Change to the beginning at the end Responsible for modifying the current pixel displacement value left startTimer Start ,killTimer end
void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE;
// Redraw events , According to the displacement left Show text
void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE;
// Setting text in 、 The scaling event calls the adaptive function twice
void setText(const QString &txt);
// Set pictures , Mainly lt Set back 0, Make it return to normal picture display
void setPixmap(const QPixmap &pix);
// Window change event
void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE;
// According to the given value , Modify the scrolling speed sp Is how many pixels to scroll at a time ,st How many seconds trigger a scroll
void setspeed(int sp=10,int st=300);
private:
int timerId; // Timer id
int text_wpixel; // Stored current label The pixel horizontal length of the inner string
int speedt;// How often is scrolling triggered
int spixel;// How many pixels to scroll at a time
int left;// Indicates the current pixel scrolling amount
QString blank;// Space
int blank_wp;// The pixel width of the space
int start_scroll;
uint8_t flag; // Determine whether scrolling should be enabled 0 no 1 really
bool isFixedScroll;
};
#endif // SCROLL_LABEL_H
cpp file :
#include “scrolllabel.h”
ScrollLabel::ScrollLabel(QWidget *parent):QLabel(parent)
{
timerId = -1;// Timer ID
text_wpixel = 0; // The pixel length of the text
speedt = 80;// How often is scrolling triggered
spixel = 10;// How many pixels to scroll at a time
//start_scroll = this->width();// Saved the initial width of the form , Avoid window changes without scrolling
flag = 0;// Default not to handle
isFixedScroll = false; // By default, it scrolls when the text is too long
}
ScrollLabel::~ScrollLabel()
{
if(timerId >= 0)
killTimer(timerId);
}
// Setting text in 、 The scaling event calls the adaptive function twice
void ScrollLabel::setText(const QString &txt)
{
if(Qt::mightBeRichText(txt))// Determine whether it is rich text
flag = 0; //0 Don't deal with , Directly display with the original painting event , Use this when it belongs to rich text 1 Left to right 2 Up and down
else
flag = 1;
QLabel::setText(txt);
upateLabelRollingState();
}
// Set pictures , Mainly lt Set back 0, Make it return to normal picture display
void ScrollLabel::setPixmap(const QPixmap &pix)
{
flag=0;
QLabel::setPixmap(pix);
}
// Window change event
void ScrollLabel::resizeEvent(QResizeEvent *e)
{
QLabel::resizeEvent(e);
upateLabelRollingState();
}
// According to the given value , Modify the scrolling speed sp Is how many pixels to scroll at a time ,st How many seconds trigger a scroll
void ScrollLabel::setspeed(int sp,int st)
{
spixel = sp;
speedt = st;
upateLabelRollingState(); // Refresh the scrolling amount once
}
// Used to determine label Whether the text needs to scroll , Is the core of this function
void ScrollLabel::upateLabelRollingState()
{
// Get text size , Less than the length of the text box , No scrolling required
QFont ft = font();// Get the format of the current font , There are text size and text pixel size
QFontMetrics fm(ft); // Based on the current font format
#if QT_VERSION > QT_VERSION_CHECK(5,11,0)// According to the official documentation ,5.11 Then use the new function
text_wpixel = fm.horizontalAdvance(text() ); // Based on the current font format , Calculate the pixel width of the font
#else
text_wpixel = fm.width(text() ); // Based on the current font format , Calculate the pixel width of the font
#endif
if((flag == 1 && isFixedScroll) || ((text_wpixel > this->width() ) && flag == 1) )// ** The length or height exceeds itself label The pixel size of , Turn on scrolling *** Key judgments
{
left = 0; // Marks the current pixel scrolling amount
#if QT_VERSION > QT_VERSION_CHECK(5,11,0)// According to the official documentation ,5.11 Then use the new function
blank = " ";// Space
blank_wp = fm.horizontalAdvance(blank );// The pixel width of the space , It is convenient to calculate whether the end is reached later
#else
blank = " ";// Space
blank_wp = fm.width(blank );// The pixel width of the space
#endif
qDebug()<< "OK!";
// Turn on timer , The timer triggers the scrolling effect at a fixed time
timerId = startTimer(speedt);
}
else// Turn off text box scrolling
{
qDebug()<< "no OK!";
flag = 0; // close
if(timerId >= 0){
killTimer(timerId);
timerId = -1;
}
}
}
void ScrollLabel::setScrollLabelFont(const QString &family, int pointSize, int weight, bool italic)
{
QFont f(family,pointSize,weight,italic); // When windows The system sets the text display scale to be not 100% When , Setting the font size number here does not mean that the pixel size of the text is this number
f.setPixelSize(pointSize);
setFont(f);
}
void ScrollLabel::setFixedScrollShow(bool isOn)
{
isFixedScroll = isOn;
}
// Change the displacement regularly , Change to the beginning at the end Responsible for modifying the current pixel displacement value
void ScrollLabel::timerEvent(QTimerEvent *e)
{
if(e->timerId() == timerId && isVisible())
{
left += spixel;// (0,0) In the upper left corner , Increase the corresponding pixels each time
if((left + 20) > (text_wpixel + blank_wp) )// It means to the end
{
left = 1-( this->width() ); // To add , Negative numbers mean starting from the far right
}
//repaint();// Immediately trigger a refresh , No redundancy , But it consumes performance
update();// It won't refresh immediately , There may be event redundancy , But save performance
//update and repaint The difference between , Please have a look at QT documentation
}
QLabel::timerEvent(e);
}
// Redraw events , According to the displacement left Show text
void ScrollLabel::paintEvent(QPaintEvent *e)
{
if(flag == 0){
// Don't deal with , Call the default function of the tag directly
QLabel::paintEvent(e);
return;
}
QPainter pen(this);
// Get current label Rectangle size of
QRect rc = rect();
rc.setHeight(rc.height() /*- 2*/);
rc.setWidth(rc.width() /*- 2*/);
QString strText = blank + text();
rc.setLeft(rc.left() - left); // Modify rectangle x Axis , because left It's getting bigger ,setLeft It's getting smaller ,(0,0) In the upper left corner , Fixed left shift
pen.drawText(rc,Qt::AlignVCenter, strText);// According to the given rectangular coordinates , Draw labels
}
边栏推荐
- 关于npm install 报错问题 error 1
- QLabel 跑马灯文字显示
- [sword finger offer] 60 Points of N dice
- Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
- AvL树的实现
- 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
- Installation and management procedures
- Method of accessing mobile phone storage location permission under non root condition
- Unity资源顺序加载的一个方法
- epoll()无论涉及wait队列分析
猜你喜欢
基于ppg和fft神经网络的光学血压估计【翻译】
Binary search tree
287. 寻找重复数
人体骨骼点检测:自顶向下(部分理论)
Optical blood pressure estimation based on PPG and FFT neural network [translation]
Handwritten online chat system (principle part 1)
爬虫玩得好,牢饭吃到饱?这3条底线千万不能碰!
巨杉数据库首批入选金融信创解决方案!
多线程基础:线程基本概念与线程的创建
[matlab] Simulink the input and output variables of the same module cannot have the same name
随机推荐
Helm deploy etcd cluster
DOM简要
Echart simple component packaging
Penetration test information collection - App information
Specify flume introduction, installation and configuration
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
CSRF漏洞分析
[depth first search] Ji suanke: a joke of replacement
被疫情占据的上半年,你还好么?| 2022年中总结
MySQL查询请求的执行过程——底层原理
美庐生物IPO被终止:年营收3.85亿 陈林为实控人
[Matlab] Simulink 同一模块的输入输出的变量不能同名
Deep circulation network long-term blood pressure prediction [translation]
Blue Bridge Cup real question: one question with clear code, master three codes
Summary of performance knowledge points
bonecp使用数据源
The role of applet in industrial Internet
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
【中山大学】考研初试复试资料分享
helm部署etcd集群