您的位置:首页 > 编程语言 > Qt开发

Qt实现360桌面精灵

2016-11-22 10:48 429 查看
有两个QLabel, 一个显示静态图片, 一个显示动态图片, 相互切换显示 ..





[cpp] view
plain copy

 





#ifndef TESTCHICKEN_H  

#define TESTCHICKEN_H  

  

#include <QtWidgets/QDialog>  

#include "ui_testChicken.h"  

#include <QPoint>  

  

class QMovie;  

class testChicken : public QDialog  

{  

    Q_OBJECT  

  

public:  

    testChicken(QWidget *parent = 0);  

    ~testChicken();  

  

private slots:  

    void OnTimerOut();  

  

private:  

    virtual void mousePressEvent( QMouseEvent* e);  

    virtual void mouseMoveEvent( QMouseEvent* e);  

    virtual void mouseReleaseEvent( QMouseEvent * e);  

    bool CheckDragRegion(const QPoint &g_pos);  

  

  

private:  

    Ui::testChickenClass ui;  

    QMovie *movie;  

  

    bool mbDragEnabled;  

    QPoint mRelativePos;  

};  

  

#endif // TESTCHICKEN_H  

[cpp] view
plain copy

 





#include "testChicken.h"  

#include <QMovie>  

#include <QTimer>  

#include <QMouseEvent>  

  

testChicken::testChicken(QWidget *parent)  

    : QDialog(parent)  

{  

    ui.setupUi(this);  

  

    mbDragEnabled = false;  

    mRelativePos = QPoint(0,0);  

  

    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);  

    setAttribute(Qt::WA_TranslucentBackground);  

  

    QPixmap pixmap("AnZai.png");  

    ui.labImage->setPixmap(pixmap);  

  

    movie = new QMovie("AnZai.gif");  

    ui.labAnimation->setMovie(movie);  

    ui.labAnimation->hide();  

  

    QTimer* timer = new QTimer;  

    timer->setInterval(3000);  

    timer->start();  

  

    connect(timer, SIGNAL(timeout()), this, SLOT(OnTimerOut()));  

}  

  

testChicken::~testChicken()  

{  

  

}  

  

void testChicken::OnTimerOut()  

{  

    if(movie->state() == QMovie::Running) {  

        movie->stop();  

        ui.labAnimation->hide();  

        ui.labImage->show();  

    }  

    else {  

        movie->start();  

        ui.labAnimation->show();  

        ui.labImage->hide();  

    }  

}  

  

void testChicken::mousePressEvent( QMouseEvent* e )  

{  

    if(CheckDragRegion(e->globalPos())) {  

        mbDragEnabled = true;  

        mRelativePos= pos() - e->globalPos();  

    }  

}  

  

void testChicken::mouseMoveEvent( QMouseEvent* e )  

{  

    if(mbDragEnabled) {  

        move(e->globalPos()+ mRelativePos);  

    }  

}  

  

void testChicken::mouseReleaseEvent( QMouseEvent * e )  

{  

    if(mbDragEnabled) {  

        mbDragEnabled = false;  

    }  

}  

  

bool testChicken::CheckDragRegion( const QPoint &g_pos )  

{  

    QPoint pt = g_pos;   

    QRect rcWnd = geometry();   

    //qDebug() << "pos: " << pt << "geometry: " << rcWnd;   

    pt.setX(pt.x() - rcWnd.left());   

    pt.setY(pt.y() - rcWnd.top());   

  

    int nW = rcWnd.width();  

    int nH = rcWnd.height();   

  

    QRect rcDrag(0, 0, nW, nH);  

    if(rcDrag.contains(pt)) {         

        return true;  

    }  

  

    return false;   

}  



原文链接:http://blog.csdn.net/robertkun/article/details/27096677
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Qt 360桌面 精灵