当前位置:网站首页>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
}
边栏推荐
- Blue Bridge Cup real question: one question with clear code, master three codes
- Medical image segmentation
- Unity资源顺序加载的一个方法
- [depth first search] Ji suanke: a joke of replacement
- CSRF vulnerability analysis
- 2022.2.12
- Use cpolar to build a business website (1)
- Collection of penetration test information -- use with nmap and other tools
- Shangsilicon Valley JUC high concurrency programming learning notes (3) multi thread lock
- 文档编辑之markdown语法(typora)
猜你喜欢
随机推荐
[the 300th weekly match of leetcode]
Penetration test information collection - basic enterprise information
Method of accessing mobile phone storage location permission under non root condition
青龙面板最近的库
RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
Medical image segmentation
celery最佳实践
Cobra quick start - designed for command line programs
Estimate blood pressure according to PPG using spectral spectrum time depth neural network [turn]
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
使用map函数、split函数一行键入多个元素
Interpreting cloud native technology
Penetration test information collection - site architecture and construction
随着MapReduce job实现去加重,多种输出文件夹
2022/02/12
JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
[Sun Yat sen University] information sharing of postgraduate entrance examination and re examination
巨杉数据库首批入选金融信创解决方案!
Oracle advanced (IV) table connection explanation