php跑马灯,Qt--滚动字幕(传说中的跑马灯)【搞定】

#ifndef TICKER_H

#define TICKER_H

#include

class Ticker : public QWidget

{

Q_OBJECT

Q_PROPERTY(QString text READ text WRITE setText)

public:

Ticker(QWidget *parent = 0);

void setText(const QString &newText);

QString text() const { return myText; }

QSize sizeHint() const;

protected:

void paintEvent(QPaintEvent *event);

void timerEvent(QTimerEvent *event);

void showEvent(QShowEvent *event);

void hideEvent(QHideEvent *event);

private:

QString myText;

int offset;

int myTimerId;

};

#endif

cpp文件#include

#include "ticker.h"

Ticker::Ticker(QWidget *parent)

: QWidget(parent)

{

offset = 0;

myTimerId = 0;

}

void Ticker::setText(const QString &newText)

{

myText = newText;

update();

updateGeometry();

}

QSize Ticker::sizeHint() const

{

return fontMetrics().size(0, text());

}

void Ticker::paintEvent(QPaintEvent * /* event */)

{

QPainter painter(this);

int textWidth = fontMetrics().width(text());

if (textWidth < 1)

return;

int x = -offset;

while (x < width()) {

painter.drawText(x, 0, textWidth, height(),

Qt::AlignLeft | Qt::AlignVCenter, text());

x += textWidth;

}

}

void Ticker::showEvent(QShowEvent * /* event */)

{

myTimerId = startTimer(30);

}

void Ticker::timerEvent(QTimerEvent *event)

{

if (event->timerId() == myTimerId) {

++offset;

if (offset >= fontMetrics().width(text()))

offset = 0;

scroll(-1, 0);

} else {

QWidget::timerEvent(event);

}

}

void Ticker::hideEvent(QHideEvent * /* event */)

{

killTimer(myTimerId);

myTimerId = 0;

}

main文件#include

#include "ticker.h"

int main(int argc, char *argv[])

{

QApplication app(argc, argv);

Ticker ticker;

ticker.setWindowTitle(QObject::tr("Ticker"));

ticker.setText(QObject::tr("How long it lasted was impossible to "

"say ++ "));

ticker.show();

return app.exec();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值