• 12776阅读
  • 10回复

MarqueeLabel跑马灯标签部件 [复制链接]

上一主题 下一主题
离线XChinux
 

只看楼主 倒序阅读 楼主  发表于: 2012-07-15
关键词: Marquee
跑马灯标签部件,常用于滚动显示广告之类的,鼠标移入后停止,移出后继续,点击链接可从外部打开。没有注释,强迫用户自己去读代码消化。

MarqueeLabel.hpp
  1. #ifndef MARQUEELABEL_HPP
  2. #define MARQUEELABEL_HPP
  3. #include <QtGui/QLabel>
  4. class QPropertyAnimation;
  5. class QResizeEvent;
  6. class MarqueeLabel : public QLabel
  7. {
  8.     Q_OBJECT
  9. public:
  10.     MarqueeLabel(QWidget * parent = 0, Qt::WindowFlags f = 0);
  11.     MarqueeLabel(const QString &text, QWidget *parent = 0,
  12.             Qt::WindowFlags f = 0);
  13.     virtual ~MarqueeLabel();
  14. public slots:
  15.     void setText(const QString &text);
  16. protected:
  17.     virtual void resizeEvent(QResizeEvent *event);
  18.     virtual void leaveEvent(QEvent *event);
  19.     virtual void enterEvent(QEvent *event);
  20. private slots:
  21.     void openLink(const QString &url);
  22. private:
  23.     QPropertyAnimation *_animation;
  24. };
  25. #endif


MarqueeLabel.cpp
  1. #include <QtGui>
  2. #include <QtCore/QUrl>
  3. #include <QtConcurrentRun>
  4. #include <QtGui/QResizeEvent>
  5. #include <QtGui/QDesktopServices>
  6. #include <QtCore/QPropertyAnimation>
  7. #include "MarqueeLabel.hpp"
  8. MarqueeLabel::MarqueeLabel(QWidget * parent, Qt::WindowFlags f)
  9.     : QLabel(parent, f)
  10. {
  11.     setOpenExternalLinks(false);
  12.     _animation = new QPropertyAnimation(this, "geometry", this);
  13.     connect(this, SIGNAL(linkActivated(const QString &)),
  14.             this, SLOT(openLink(const QString &)));
  15. }
  16. MarqueeLabel::MarqueeLabel(const QString &text, QWidget *parent,
  17.         Qt::WindowFlags f )
  18.     : QLabel(text, parent, f)
  19. {
  20.     setOpenExternalLinks(false);
  21.     _animation = new QPropertyAnimation(this, "geometry", this);
  22.     connect(this, SIGNAL(linkActivated(const QString &)),
  23.             this, SLOT(openLink(const QString &)));
  24. }
  25. MarqueeLabel::~MarqueeLabel()
  26. {
  27. }
  28. void MarqueeLabel::leaveEvent(QEvent *event)
  29. {
  30.     _animation->resume();
  31.     QLabel::leaveEvent(event);
  32. }
  33. void MarqueeLabel::enterEvent(QEvent *event)
  34. {
  35.     _animation->pause();
  36.     QLabel::enterEvent(event);
  37. }
  38. void MarqueeLabel::resizeEvent(QResizeEvent *event)
  39. {
  40.     QLabel::resizeEvent(event);
  41.     disconnect(_animation, SIGNAL(finished()), _animation, SLOT(start()));
  42.     _animation->stop();
  43.     int iWidth = sizeHint().width();
  44.     int iParentWidth = parentWidget() ? parentWidget()->width() : 600;
  45.     int iHeight = event->size().height();
  46.     connect(_animation, SIGNAL(finished()), _animation, SLOT(start()));
  47.     _animation->setDuration(10000);
  48.     _animation->setStartValue(QRect(iParentWidth, 0, iWidth, iHeight));
  49.     _animation->setEndValue(QRect(-iWidth, 0, iWidth, iHeight));
  50.     _animation->start();
  51. }
  52. void MarqueeLabel::openLink(const QString &url)
  53. {
  54.     QtConcurrent::run(QDesktopServices::openUrl, QUrl(url));
  55. }
  56. void MarqueeLabel::setText(const QString &text)
  57. {
  58.     disconnect(_animation, SIGNAL(finished()), _animation, SLOT(start()));
  59.     _animation->stop();
  60.     QLabel::setText(text);
  61.     int iWidth = sizeHint().width();
  62.     int iParentWidth = parentWidget() ? parentWidget()->width() : 600;
  63.     int iHeight = height();
  64.     connect(_animation, SIGNAL(finished()), _animation, SLOT(start()));
  65.     _animation->setDuration(10000);
  66.     _animation->setStartValue(QRect(iParentWidth, 0, iWidth, iHeight));
  67.     _animation->setEndValue(QRect(-iWidth, 0, iWidth, iHeight));
  68.     _animation->start();
  69. }
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线ppdayz

只看该作者 1楼 发表于: 2012-07-16
先mark下,等下午有空在看看
离线kimtaikee

只看该作者 2楼 发表于: 2012-07-16
两个构造函数里面的代码太重复了,写到一个工具函数去看着还舒服点

离线toby520

只看该作者 3楼 发表于: 2012-10-08
学习中
QtQML多多指教开发社区 http://qtclub.heilqt.com
将QtCoding进行到底
关注移动互联网,关注金融
开发跨平台客户端,服务于金融行业
专业定制界面
群号:312125701   373955953(qml控件定做)
离线cfxks1989

只看该作者 4楼 发表于: 2012-10-09
坐标定位先
离线phpqinsir
只看该作者 5楼 发表于: 2012-12-12
mark
qtcn.org是我见过最热心的论坛,也是解决问题率最高的论坛。希望,我的问题能让更多的人少走弯路。
离线随心所遇

只看该作者 6楼 发表于: 2014-11-20
不错,谢谢了
我思顾我在
离线随心所遇

只看该作者 7楼 发表于: 2014-11-20
为什么QPropertyAnimation的对象要分别在setText和resizeEvent函数中实现两次,仅仅在resizeEvent添加行不行?
我思顾我在
离线eric584930

只看该作者 8楼 发表于: 2015-03-04
后面拖着小尾巴...
生活就是这样
离线shasidaran

只看该作者 9楼 发表于: 2015-03-09
能不能上一张效果图。。。
离线新手崛起

只看该作者 10楼 发表于: 2017-08-24
跑马灯眼 上gif效果图更好
只有想不到,没有做不到的
快速回复
限100 字节
 
上一个 下一个