-
UID:2
-
- 注册时间2004-11-08
- 最后登录2025-04-28
- 在线时间7016小时
-
- 发帖11242
- 搜Ta的帖子
- 精华61
- 金钱147522
- 威望9967
- 贡献值702
- 好评度8189
-
访问TA的空间加好友用道具
|
跑马灯标签部件,常用于滚动 显示广告之类的,鼠标移入后停止,移出后继续,点击链接可从外部打开。 没有注释,强迫用户自己去读代码消化。 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();
- }
|