有两个QLabel, 一个显示静态图片, 一个显示动态图片, 相互切换显示 ..
- #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
- #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