XChinux |
2012-07-15 12:58 |
MarqueeLabel跑马灯标签部件
跑马灯标签部件,常用于滚动显示广告之类的,鼠标移入后停止,移出后继续,点击链接可从外部打开。没有注释,强迫用户自己去读代码消化。
MarqueeLabel.hpp
#ifndef MARQUEELABEL_HPP #define MARQUEELABEL_HPP
#include <QtGui/QLabel>
class QPropertyAnimation; class QResizeEvent;
class MarqueeLabel : public QLabel { Q_OBJECT public: MarqueeLabel(QWidget * parent = 0, Qt::WindowFlags f = 0); MarqueeLabel(const QString &text, QWidget *parent = 0, Qt::WindowFlags f = 0); virtual ~MarqueeLabel(); public slots: void setText(const QString &text); protected: virtual void resizeEvent(QResizeEvent *event); virtual void leaveEvent(QEvent *event); virtual void enterEvent(QEvent *event); private slots: void openLink(const QString &url); private: QPropertyAnimation *_animation; }; #endif
MarqueeLabel.cpp
#include <QtGui> #include <QtCore/QUrl> #include <QtConcurrentRun> #include <QtGui/QResizeEvent> #include <QtGui/QDesktopServices> #include <QtCore/QPropertyAnimation> #include "MarqueeLabel.hpp"
MarqueeLabel::MarqueeLabel(QWidget * parent, Qt::WindowFlags f) : QLabel(parent, f) { setOpenExternalLinks(false); _animation = new QPropertyAnimation(this, "geometry", this); connect(this, SIGNAL(linkActivated(const QString &)), this, SLOT(openLink(const QString &))); }
MarqueeLabel::MarqueeLabel(const QString &text, QWidget *parent, Qt::WindowFlags f ) : QLabel(text, parent, f) { setOpenExternalLinks(false); _animation = new QPropertyAnimation(this, "geometry", this); connect(this, SIGNAL(linkActivated(const QString &)), this, SLOT(openLink(const QString &))); }
MarqueeLabel::~MarqueeLabel() { }
void MarqueeLabel::leaveEvent(QEvent *event) { _animation->resume(); QLabel::leaveEvent(event); }
void MarqueeLabel::enterEvent(QEvent *event) { _animation->pause(); QLabel::enterEvent(event); }
void MarqueeLabel::resizeEvent(QResizeEvent *event) { QLabel::resizeEvent(event); disconnect(_animation, SIGNAL(finished()), _animation, SLOT(start())); _animation->stop();
int iWidth = sizeHint().width(); int iParentWidth = parentWidget() ? parentWidget()->width() : 600; int iHeight = event->size().height(); connect(_animation, SIGNAL(finished()), _animation, SLOT(start())); _animation->setDuration(10000); _animation->setStartValue(QRect(iParentWidth, 0, iWidth, iHeight)); _animation->setEndValue(QRect(-iWidth, 0, iWidth, iHeight)); _animation->start(); }
void MarqueeLabel::openLink(const QString &url) { QtConcurrent::run(QDesktopServices::openUrl, QUrl(url)); }
void MarqueeLabel::setText(const QString &text) { disconnect(_animation, SIGNAL(finished()), _animation, SLOT(start())); _animation->stop(); QLabel::setText(text);
int iWidth = sizeHint().width(); int iParentWidth = parentWidget() ? parentWidget()->width() : 600; int iHeight = height(); connect(_animation, SIGNAL(finished()), _animation, SLOT(start())); _animation->setDuration(10000); _animation->setStartValue(QRect(iParentWidth, 0, iWidth, iHeight)); _animation->setEndValue(QRect(-iWidth, 0, iWidth, iHeight)); _animation->start(); }
|
|