查看完整版本: [-- MarqueeLabel跑马灯标签部件 --]

QTCN开发网 -> Qt代码秀 -> MarqueeLabel跑马灯标签部件 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

XChinux 2012-07-15 12:58

MarqueeLabel跑马灯标签部件

跑马灯标签部件,常用于滚动显示广告之类的,鼠标移入后停止,移出后继续,点击链接可从外部打开。没有注释,强迫用户自己去读代码消化。

MarqueeLabel.hpp

  1. #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

  1. #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();
    }

ppdayz 2012-07-16 09:11
先mark下,等下午有空在看看

kimtaikee 2012-07-16 10:10
两个构造函数里面的代码太重复了,写到一个工具函数去看着还舒服点

toby520 2012-10-08 15:56
学习中

cfxks1989 2012-10-09 10:52
坐标定位先

phpqinsir 2012-12-12 12:35
mark

随心所遇 2014-11-20 11:49
不错,谢谢了

随心所遇 2014-11-20 17:08
为什么QPropertyAnimation的对象要分别在setText和resizeEvent函数中实现两次,仅仅在resizeEvent添加行不行?

eric584930 2015-03-04 15:09
后面拖着小尾巴...

shasidaran 2015-03-09 09:41
能不能上一张效果图。。。

新手崛起 2017-08-24 18:10
跑马灯眼 上gif效果图更好


查看完整版本: [-- MarqueeLabel跑马灯标签部件 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled