// 这里我就已一句话为例,如果存在什么换行之类的请参考 “QT 字幕滚动(上下滚动)” 进行处理
// 这里我就贴上代码,具体就不详细说明
----------------------------- HScrolllabel.h---------------------------------
#ifndef HSCROLLLABEL_H
#define HSCROLLLABEL_H
#include <QLabel>
class HScrollLabel : public QLabel
{
Q_OBJECT
public:
explicit HScrollLabel(QWidget *parent = 0, Qt::WindowFlags f=0);
virtual ~HScrollLabel(){}
public slots:
void setText(const QString &);
signals:
void currentTextChanged();
protected:
void showEvent(QShowEvent *);
void paintEvent(QPaintEvent *);
void timerEvent(QTimerEvent *);
void hideEvent(QHideEvent *);
private slots:
void metrics();
void startScroll();
private:
int getWidth();
private:
int offset;
int timerID;
int textWidth;
bool flag;
};
#endif // HSCOLLLABEL_H
----------------------------- HScrolllabel.cpp---------------------------------
#include "hscrolllabel.h"
#include <QPainter>
#include <QTimerEvent>
#include <QTimer>
HScrollLabel::HScrollLabel(QWidget *parent, Qt::WindowFlags f) :
QLabel(parent,f)
{
flag = false;
offset = timerID = textWidth = 0;
connect(this, SIGNAL(currentTextChanged()), SLOT(metrics()));
}
void HScrollLabel::setText(const QString &curText)
{
if (text() != curText) {
QLabel::setText(curText);
QLabel::setAlignment(Qt::AlignCenter);
emit currentTextChanged();
}
}
void HScrollLabel::startScroll()
{
timerID = startTimer(50);
}
void HScrollLabel::metrics()
{
flag = false;
offset = 0;
textWidth = 0;
if(getWidth() < width())
return;
flag = true;
textWidth = getWidth() +width();
QTimer::singleShot(500, this, SLOT(startScroll()));
}
int HScrollLabel::getWidth()
{
int fm_width = fontMetrics().width(text());
return fm_width;
}
void HScrollLabel::showEvent(QShowEvent *)
{
metrics();
}
void HScrollLabel::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//if (textWidth < 1) return;
if(!flag)
{
painter.drawText(0, 0, width(), height() ,
Qt::AlignHCenter | Qt::AlignVCenter, text());
return;
}
int x = -offset ;
while (x < width()) {
painter.drawText(x, 0, textWidth, height() ,
Qt::AlignHCenter | Qt::AlignVCenter, text());
x += textWidth;
}
}
void HScrollLabel::timerEvent(QTimerEvent *event)
{
if (event->timerId() == timerID) {
++offset;
if (offset >= textWidth)
offset = 0;
scroll(-3, 0); // 这里是从右向左滚动
} else {
QWidget::timerEvent(event);
}
}
void HScrollLabel::hideEvent(QHideEvent *)
{
offset = 0;
if (timerID) killTimer(timerID);
// 这里我就贴上代码,具体就不详细说明
----------------------------- HScrolllabel.h---------------------------------
#ifndef HSCROLLLABEL_H
#define HSCROLLLABEL_H
#include <QLabel>
class HScrollLabel : public QLabel
{
Q_OBJECT
public:
explicit HScrollLabel(QWidget *parent = 0, Qt::WindowFlags f=0);
virtual ~HScrollLabel(){}
public slots:
void setText(const QString &);
signals:
void currentTextChanged();
protected:
void showEvent(QShowEvent *);
void paintEvent(QPaintEvent *);
void timerEvent(QTimerEvent *);
void hideEvent(QHideEvent *);
private slots:
void metrics();
void startScroll();
private:
int getWidth();
private:
int offset;
int timerID;
int textWidth;
bool flag;
};
#endif // HSCOLLLABEL_H
----------------------------- HScrolllabel.cpp---------------------------------
#include "hscrolllabel.h"
#include <QPainter>
#include <QTimerEvent>
#include <QTimer>
HScrollLabel::HScrollLabel(QWidget *parent, Qt::WindowFlags f) :
QLabel(parent,f)
{
flag = false;
offset = timerID = textWidth = 0;
connect(this, SIGNAL(currentTextChanged()), SLOT(metrics()));
}
void HScrollLabel::setText(const QString &curText)
{
if (text() != curText) {
QLabel::setText(curText);
QLabel::setAlignment(Qt::AlignCenter);
emit currentTextChanged();
}
}
void HScrollLabel::startScroll()
{
timerID = startTimer(50);
}
void HScrollLabel::metrics()
{
flag = false;
offset = 0;
textWidth = 0;
if(getWidth() < width())
return;
flag = true;
textWidth = getWidth() +width();
QTimer::singleShot(500, this, SLOT(startScroll()));
}
int HScrollLabel::getWidth()
{
int fm_width = fontMetrics().width(text());
return fm_width;
}
void HScrollLabel::showEvent(QShowEvent *)
{
metrics();
}
void HScrollLabel::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//if (textWidth < 1) return;
if(!flag)
{
painter.drawText(0, 0, width(), height() ,
Qt::AlignHCenter | Qt::AlignVCenter, text());
return;
}
int x = -offset ;
while (x < width()) {
painter.drawText(x, 0, textWidth, height() ,
Qt::AlignHCenter | Qt::AlignVCenter, text());
x += textWidth;
}
}
void HScrollLabel::timerEvent(QTimerEvent *event)
{
if (event->timerId() == timerID) {
++offset;
if (offset >= textWidth)
offset = 0;
scroll(-3, 0); // 这里是从右向左滚动
} else {
QWidget::timerEvent(event);
}
}
void HScrollLabel::hideEvent(QHideEvent *)
{
offset = 0;
if (timerID) killTimer(timerID);
}