[转载]Qt 水平滚动字幕
2016-09-17 16:56
429 查看
原文地址:Qt 水平滚动字幕作者:漠北
// widget.h"
#ifndef WIDGET_H
// widget.h"
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText)
public:
explicit Widget(QWidget *parent = 0);
~Widget();
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;
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
// widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <qcoreevent.h>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
offset = 0;
myTimerId = 0;
}
Widget::~Widget()
{
delete ui;
}
void Widget::setText(const QString &newText)
{
myText = newText;
update();
updateGeometry();
}
QSize Widget::sizeHint() const
{
return fontMetrics().size(0, text());
}
void Widget::paintEvent(QPaintEvent * )
{
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 Widget::showEvent(QShowEvent * )
{
myTimerId = startTimer(30);
}
void Widget::timerEvent(QTimerEvent *event)
{
if (event->timerId() == myTimerId) {
++offset;
if (offset >= fontMetrics().width(text()))
offset = 0;
scroll(-1, 0);
} else {
QWidget::timerEvent(event);
}
}
void Widget::hideEvent(QHideEvent * )
{
killTimer(myTimerId);
myTimerId = 0;
}
// main.cp
#include <QtGui/QApplication>
#include "widget.h"
#include <QLabel>
#include <QGridLayout>
#include <QTextCodec>
#include <QIcon>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setWindowTitle(QObject::tr("Ticker"));
w.setText(QObject::tr("How long it lasted was impossible to "
"say ++ "));
w.show();
return a.exec();
}
相关文章推荐
- Qt 水平滚动字幕2
- Qt(QLabel)实现滚动字幕
- qt实现无缝滚动字幕,刷新间隔任意调节
- 水平滚动字幕的jquery插件和原生代码
- 跑马灯(字幕水平滚动)的两种实现方式
- Qt 5滚动字幕(左/右)+闪烁效果(QLabel控件显示)
- 水平方向滚动字幕的实现
- 水平滚动字幕的jquery插件和原生代码
- marquee插件水平滚动字幕循环滚动
- Qt实现滚动字幕效果
- 三种方式实现字幕水平滚动(走马灯),而且定时更新
- QT里滚动字幕如何做
- QT 实现字幕滚动(上下滚动)
- <Qt C++>滚动字幕
- Qt 字幕滚动程序
- Qt实现滚动字幕
- QT滚动字幕代码(包括中英文截取)
- QT制作字幕滚动效果
- 水平滚动字幕的jquery插件和原生代码
- 解决qt滚动字幕cpu占用过高的问题