当前位置:网站首页>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
}
边栏推荐
- test about BinaryTree
- CSRF vulnerability analysis
- Jushan database was among the first batch of financial information innovation solutions!
- Optical blood pressure estimation based on PPG and FFT neural network [translation]
- [Matlab] Simulink 同一模块的输入输出的变量不能同名
- Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
- None of the strongest kings in the monitoring industry!
- [depth first search] Ji suanke: a joke of replacement
- 2022/02/12
- About NPM install error 1
猜你喜欢
能源行业的数字化“新”运维
Execution process of MySQL query request - underlying principle
渲大师携手向日葵,远控赋能云渲染及GPU算力服务
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Use cpolar to build a business website (1)
Collection of penetration test information -- use with nmap and other tools
AvL树的实现
CSRF vulnerability analysis
Oracle advanced (IV) table connection explanation
随机推荐
Reptiles have a good time. Are you full? These three bottom lines must not be touched!
徐翔妻子应莹回应“股评”:自己写的!
Crawling data encounters single point login problem
图之广度优先遍历
On AAE
test about BinaryTree
Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
AcWing 3537.树查找 完全二叉树
Nuc11 cheetah Canyon setting U disk startup
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
多线程基础:线程基本概念与线程的创建
[Sun Yat sen University] information sharing of postgraduate entrance examination and re examination
wx小程序学习笔记day01
MySQL查询请求的执行过程——底层原理
Introduction and case analysis of Prophet model
44 colleges and universities were selected! Publicity of distributed intelligent computing project list
bonecp使用数据源
Interpreting cloud native technology
根据PPG估算血压利用频谱谱-时间深度神经网络【翻】