![在这里插入图片描述](https://img-blog.csdnimg.cn/20190316182227672.gif)
#include "nbasemarqueelabel.h"
#include <QDebug>
NBaseMarqueeLabel::NBaseMarqueeLabel(QWidget *parent, Qt::WindowFlags f)
:QLabel(parent, f)
{
marqueeMargin = 0;
}
NBaseMarqueeLabel::NBaseMarqueeLabel(const QString &text, QWidget *parent, Qt::WindowFlags f)
:QLabel(text, parent, f)
{
marqueeMargin = 0;
}
NBaseMarqueeLabel::~NBaseMarqueeLabel()
{
if(timerId > 0){
killTimer(timerId);
timerId = 0;
}
}
int NBaseMarqueeLabel::interval() const
{
return mInterval;
}
bool NBaseMarqueeLabel::isActive() const
{
return active;
}
NBaseMarqueeLabel::Direction NBaseMarqueeLabel::direction() const
{
return mDirection;
}
//对其方式
void NBaseMarqueeLabel::setAlignment(Qt::Alignment align)
{
switch (mDirection)
{
case RightToLeft:
QLabel::setAlignment(Qt::AlignLeft
| (align & Qt::AlignVertical_Mask));
break;
case BottomToTop:
default:
QLabel::setAlignment(Qt::AlignTop
| (align & Qt::AlignHorizontal_Mask));
}
}
void NBaseMarqueeLabel::reset()
{
if (timerId != 0)
{
killTimer(timerId);
timerId = 0;
}
bool bActiveChanged = false;
if (active)
{
active = false;
bActiveChanged = true;
}
marqueeMargin = 0;
setContentsMargins(0, 0, 0, 0);
update();
if (bActiveChanged)
{
//Q_EMIT activeChanged(active);
}
}
void NBaseMarqueeLabel::setActive(bool active)
{
qDebug() << "NBaseMarqueeLabel::setActive";
if (active == active)
{
return;
}
if (active)
{
start();
}
else
{
stop();
}
}
void NBaseMarqueeLabel::setInterval(int msec)
{
qDebug() << "NBaseMarqueeLabel::setInterval";
if (msec < 1)
{
return;
}
if (mInterval != msec)
{
mInterval = msec;
if (timerId != 0)
{
killTimer(timerId);
timerId = startTimer(mInterval);
}
//Q_EMIT intervalChanged(mInterval);
}
}
/*******************
*
*
*
*******************/
void NBaseMarqueeLabel::start()
{
bool bActiveChanged = false;
if (!active)
{
active = true;
bActiveChanged = true;
}
if (!mouseIn)
{
if (timerId == 0)
{
timerId = startTimer(mInterval);
}
//设置左侧、顶部、右侧和底部边距,以便在布局周围使用。
setContentsMargins(0, 0, 0, 0);
}
if (bActiveChanged)
{
//Q_EMIT activeChanged(active);
}
}
void NBaseMarqueeLabel::stop()
{
bool bActiveChanged = false;
if (active)
{
active = false;
bActiveChanged = true;
}
if (!mouseIn)
{
if (timerId != 0)
{
killTimer(timerId);
timerId = 0;
}
switch (mDirection)
{
case RightToLeft:
setContentsMargins(marqueeMargin, 0, 0, 0);
break;
case BottomToTop:
default:
setContentsMargins(0, marqueeMargin, 0, 0);
}
}
if (bActiveChanged)
{
// Q_EMIT activeChanged(active);
}
}
void NBaseMarqueeLabel::setDirection(NBaseMarqueeLabel::Direction param_direciton)
{
if (param_direciton == mDirection)
{
return;
}
mDirection = param_direciton;
switch (mDirection)
{
case RightToLeft:
setAlignment(Qt::AlignLeft
| (alignment() & Qt::AlignVertical_Mask));
break;
case BottomToTop:
default:
setAlignment(Qt::AlignTop
| (alignment() & Qt::AlignHorizontal_Mask));
}
marqueeMargin = 0;
setContentsMargins(0, 0, 0, 0);
update();
// Q_EMIT directionChanged(mDirection);
}
void NBaseMarqueeLabel::enterEvent(QEvent *event)
{
qDebug() << "NBaseMarqueeLabel::enterEvent";
mouseIn = true;
if (active)
{
if (timerId != 0)
{
killTimer(timerId);
timerId = 0;
}
switch (mDirection)
{
case RightToLeft:
setContentsMargins(marqueeMargin, 0, 0, 0);
break;
case BottomToTop:
default:
setContentsMargins(0, marqueeMargin, 0, 0);
}
}
QLabel::enterEvent(event);
}
void NBaseMarqueeLabel::leaveEvent(QEvent *event)
{
qDebug() << "NBaseMarqueeLabel::leaveEvent";
mouseIn = false;
if (active)
{
if (timerId == 0)
{
timerId = startTimer(mInterval);
}
setContentsMargins(0, 0, 0, 0);
}
QLabel::leaveEvent(event);
}
void NBaseMarqueeLabel::resizeEvent(QResizeEvent *event)
{
QLabel::resizeEvent(event);
int w;
switch (mDirection)
{
case RightToLeft:
w = event->size().width();
break;
case BottomToTop:
default:
w = event->size().height();
}
if (marqueeMargin > w)
{
marqueeMargin = w;
update();
}
}
/*******************
* 函数名:timerEvent
* 功能:定时器事件
* 参数:
* 返回值:
*******************/
void NBaseMarqueeLabel::timerEvent(QTimerEvent *event)
{
QLabel::timerEvent(event);
if (timerId != 0 && event->timerId() == timerId)
{
marqueeMargin = marqueeMargin - 4;
int w, w2;
switch (mDirection)
{
case RightToLeft:
w = sizeHint().width();
w2 = width();
break;
case BottomToTop:
default:
w = sizeHint().height();
w2 = height();
}
if (marqueeMargin < 0 && -marqueeMargin > w)
{
marqueeMargin = w2;
}
update();
}
}
void NBaseMarqueeLabel::paintEvent(QPaintEvent *event)
{
switch (mDirection)
{
case RightToLeft:
setContentsMargins(marqueeMargin, 0, 0, 0);
break;
case BottomToTop:
default:
setContentsMargins(0, marqueeMargin, 0, 0);
}
QLabel::paintEvent(event);
if (timerId != 0)
{
setContentsMargins(0, 0, 0, 0);
}
}
#ifndef MARQUEES_H
#define MARQUEES_H
#include <QThread>
#include <QLabel>
class marquees;
/*跑马灯线程类*/
class marquees_thread: public QThread
{
Q_OBJECT
public:
explicit marquees_thread(marquees *marquee, QObject * parent = 0);
~marquees_thread();
private:
protected:
void run();
private:
marquees *marquee;
signals:
void change();
};
/*跑马灯实现类*/
class marquees: public QLabel
{
Q_OBJECT
Q_ENUMS(Direction)
public:
enum Direction
{
RightToLeft = 0, //从右向左
BottomToTop = 1, //从下到上
LeftToRight = 2, //从左向右
TopToBottom = 3 //从上到下
};
public:
marquees(QWidget * parent = 0, Qt::WindowFlags f = 0);
marquees(const QString &text, QWidget *parent = 0 ,Qt::WindowFlags f = 0);
virtual ~marquees();
public:
Direction direction() const; //获取当前Direction数据
void setAlignment(Qt::Alignment align); //设置对齐方式
public:
void setDirection(Direction param_direciton); //设置文字浮动方向
private slots:
void change(); //接收信号的槽函数
protected:
virtual void paintEvent(QPaintEvent *event); //绘图事件
private:
int marqueeMargin; //位移参数
Direction mDirection; //文字浮动方向类型
};
#endif // MARQUEES_H