左右走马灯均正常显示,目前用git还有问题,暂时保存在日志。
ticker.h
#ifndef TICKER_H
#define TICKER_H
#include <QWidget>
QT_BEGIN_NAMESPACE
class QHideEvent;
class QShowEvent;
class QTimerEvent;
class QPaintEvent;
QT_END_NAMESPACE
//测试git
/**
@brief 走马灯实例,可以左/右双向
*/
class Ticker : public QWidget
{
Q_OBJECT
//Q_PROPERTY(QString my_text_ READ getText WRITE setText)
public:
explicit Ticker(QWidget *parent = 0);
virtual QSize sizeHint() const;
/**
*@author qioawei
*@date 2018-05-10
*@version 1.0
*@brief update the content in text
*@called by
*@param text the showed content
**/
void setText(const QString& text);
QString getText() const;
protected:
/**
*@author qioawei
*@date 2018-05-10
*@version 1.0
*@brief showing window while calling this window
*@called by
*@param ev show event
**/
virtual void showEvent(QShowEvent* ev);
/**
*@author qioawei
*@date 2018-05-10
*@version 1.0
*@brief
*@called by
*@param
*@return
**/
virtual void hideEvent(QHideEvent* ev);
virtual void timerEvent(QTimerEvent* ev);
virtual void paintEvent(QPaintEvent* ev);
private:
void leftScrollTimerEvent(QTimerEvent* ev);
void leftScrollPaintEvent(QPaintEvent* ev);
void rightScrollTimerEvent(QTimerEvent* ev);
void rightScrollPaintEvent(QPaintEvent* ev);
private:
//显示的文本内容
QString my_text_;
//走马灯的偏移量
int offset_;
//定义的时钟id
int my_timer_id_;
static int move_offset_for_right_;
};
#endif // TICKER_H
ticker.cpp
#include <QHideEvent>
#include <QShowEvent>
#include <QTimerEvent>
#include <QPainter>
#include <QPaintEvent>
#include <QtDebug>
//system
#include "ticker.h"
int Ticker::move_offset_for_right_(0);
Ticker::Ticker(QWidget *parent) :
QWidget(parent),
my_text_("+ABCDEFGHIJKL_"),
offset_(0),
my_timer_id_(0)
{
setAttribute(Qt::WA_DeleteOnClose);
}
QSize Ticker::sizeHint() const
{
//2倍高度
/*int height = fontMetrics().size(Qt::TextSingleLine, getText()).height();
int width = fontMetrics().size(Qt::TextSingleLine, getText()).width();
return QSize(width * 2.3, 5 * height);*/
//根据文本内容,设定文本框显示尺寸
return (fontMetrics().size(Qt::TextSingleLine, getText()));
}
void Ticker::setText(const QString& text)
{
my_text_ = text;
//更新文本,刷新显示
update();
//布局管理器更新布局设置
updateGeometry();
}
QString Ticker::getText() const
{
return my_text_;
}
void Ticker::showEvent(QShowEvent *ev)
{
Q_UNUSED(ev)
//创建timer,并返回timer的id
my_timer_id_ = startTimer(300, Qt::PreciseTimer);
}
void Ticker::hideEvent(QHideEvent *ev)
{
Q_UNUSED(ev)
//根据timer的id终止timer
killTimer(my_timer_id_);
my_timer_id_ = 0;
}
void Ticker::timerEvent(QTimerEvent *ev)
{
//leftScrollTimerEvent(ev);
rightScrollTimerEvent(ev);
}
void Ticker::paintEvent(QPaintEvent *ev)
{
//leftScrollPaintEvent(ev);
rightScrollPaintEvent(ev);
}
void Ticker::leftScrollTimerEvent(QTimerEvent *ev)
{
if (ev->timerId() == my_timer_id_) {
++offset_;
if (fontMetrics().width(getText()) <= offset_) {
/**
当窗体的左移量offset_等于文本width时,将窗体“整体”右移offset_,在文本框中可见最左边是“第二个”文本内容,所以
将scroll方法参数重置为(0, 0),避免窗体不断左移。
*/
scroll(offset_, 0);
offset_ = 0;
}
scroll(-1, 0);
} else {
QWidget::timerEvent(ev);
}
}
void Ticker::leftScrollPaintEvent(QPaintEvent *ev)
{
Q_UNUSED(ev)
QPainter painter(this);
const int text_width(fontMetrics().width(getText()));
const int text_height(fontMetrics().height());
if (text_width < 1) {
return;
}
int x_coordinate(-offset_);
while (width() > x_coordinate) {
painter.drawText(x_coordinate,
0,
text_width,
text_height,
Qt::AlignHCenter | Qt::AlignVCenter,
getText());
x_coordinate += text_width;
}
}
void Ticker::rightScrollTimerEvent(QTimerEvent *ev)
{
if (ev->timerId() == my_timer_id_) {
++offset_;
if (fontMetrics().width(getText()) <= offset_) {
scroll(-offset_, 0);
offset_ = 0;
}
scroll(1, 0);
} else {
QWidget::timerEvent(ev);
}
}
void Ticker::rightScrollPaintEvent(QPaintEvent *ev)
{
Q_UNUSED(ev)
QPainter painter(this);
//获取文本宽度,高度
const int text_width(fontMetrics().width(getText()));
const int text_height(fontMetrics().height());
//文本为空,无内容显示
if (1 > text_width) {
return;
}
//获取文本框及偏移量的长度和(偏移量是不断变化的)
int x_coordinate(width() + offset_);
while (x_coordinate > -text_width) {
painter.drawText(x_coordinate,
0,
text_width,
text_height,
Qt::AlignRight | Qt::AlignVCenter,
getText());
x_coordinate -= text_width;
}
}