当前位置:网站首页>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
}
边栏推荐
- Jushan database was among the first batch of financial information innovation solutions!
- RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
- 徐翔妻子应莹回应“股评”:自己写的!
- Method of accessing mobile phone storage location permission under non root condition
- A method of sequentially loading Unity Resources
- Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
- 用友OA漏洞学习——NCFindWeb 目录遍历漏洞
- 提前解锁 2 大直播主题!今天手把手教你如何完成软件包集成?|第 29-30 期
- 手写一个的在线聊天系统(原理篇1)
- Hongke shares | plate by plate ar application in Beijing Winter Olympics
猜你喜欢

Blue Bridge Cup real question: one question with clear code, master three codes

Method of accessing mobile phone storage location permission under non root condition
![A wearable arm device for night and sleeveless blood pressure measurement [translation]](/img/fd/947a38742ab1c4009ec6aa7405a573.png)
A wearable arm device for night and sleeveless blood pressure measurement [translation]

Jushan database was among the first batch of financial information innovation solutions!

Describe the process of key exchange

Oracle advanced (IV) table connection explanation

人体骨骼点检测:自顶向下(部分理论)

Handwritten online chat system (principle part 1)
![Optical blood pressure estimation based on PPG and FFT neural network [translation]](/img/88/2345dac73248a5f0f9fa3142ca0397.png)
Optical blood pressure estimation based on PPG and FFT neural network [translation]

CSRF vulnerability analysis
随机推荐
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
Jdbc driver, c3p0, druid and jdbctemplate dependent jar packages
atcoder它A Mountaineer
线代笔记....
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
上海部分招工市場對新冠陽性康複者拒絕招錄
Understanding disentangling in β- VAE paper reading notes
2022.2.12
Some understandings of tree LSTM and DGL code implementation
JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
多线程基础:线程基本概念与线程的创建
Jushan database was among the first batch of financial information innovation solutions!
用于远程医疗的无创、无袖带血压测量【翻译】
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
图之广度优先遍历
Self supervised heterogeneous graph neural network with CO comparative learning
Picture zoom Center
用友OA漏洞学习——NCFindWeb 目录遍历漏洞