码迷,mamicode.com
首页 > 其他好文 > 详细

Qt制作360安仔精灵

时间:2014-06-05 02:42:59      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:qt   360安仔精灵   

有两个QLabel, 一个显示静态图片, 一个显示动态图片, 相互切换显示 ..

bubuko.com,布布扣bubuko.com,布布扣

#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://download.csdn.net/detail/robertkun/7403735

Qt制作360安仔精灵,布布扣,bubuko.com

Qt制作360安仔精灵

标签:qt   360安仔精灵   

原文地址:http://blog.csdn.net/robertkun/article/details/27096677

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!