Qt 动画开始停止

 

 

.h文件

#ifndef ONTOPBOX_H
#define ONTOPBOX_H

#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QMediaPlayer>
#include "ui_OnTopBox.h"
#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
class SocketRecvStates;
class OnTopBox : public QWidget
{
	Q_OBJECT

public:
	static OnTopBox *GetInstance();
protected:
	void mousePressEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *event);
	void paintEvent(QPaintEvent *event);
private:
	OnTopBox(QWidget *parent = 0);
	~OnTopBox();
	void readFile();
	void openMovFile();
	void initAnimalshow();
	void initAnimalHide();

	public slots:
	void slotWaitingforResult();
	void animationHideFinished();
	void slotUpdateResult(QString);
	void slotStartTimer(bool);
	void slotSleep();
signals:
	void sigSleep();
private:
	Ui::OnTopBox ui;
	QColor bgColor;
	QPoint m_startPos;
	bool m_bflag ;
	QMovie *movie;
	QTimer *m_timer;
	bool m_result;
	QTime current_time;
	SocketRecvStates * pSocket;
	QMediaPlaylist * _pMediaPlaylist;
	QMediaPlayer *_pMediaPlayer;
	QPropertyAnimation *animationShow;
	QPropertyAnimation *animationHide;
	QParallelAnimationGroup *animalGroup;
};

#endif // ONTOPBOX_H

 

 .cpp文件 

#include "OnTopBox.h"
 
#include <QFile>
#include <QDomDocument>
#include <QMessageBox>
#include <QTextStream>
#include <QPainter>
#include <QMouseEvent>
#include <QMovie>
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QDesktopWidget>
#include <windows.h>
#include <QTime>
#include "SocketRecvStates.h"
OnTopBox * OnTopBox::GetInstance()
{
	static OnTopBox *pInstance = NULL;
	if (pInstance == NULL)
	{
		pInstance = new OnTopBox;
		
	}
	return pInstance;
}
 
OnTopBox::OnTopBox(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	//this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint /*| Qt::WindowStaysOnTopHint*/);
	animalGroup = NULL;
	animationHide = NULL;
	this->setAttribute( Qt::WA_TranslucentBackground );
	this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
	bgColor = QColor(3, 3, 27, 200);
 
	m_bflag = false;
	m_timer = new QTimer(this);
	connect(m_timer, SIGNAL(timeout()), this, SLOT(slotWaitingforResult()));
	movie = new QMovie(":/OnTopBox/Resources/xuanli.gif");
	movie->setScaledSize(QSize(299, 104));
	ui.label->setMovie(movie);
	movie->start();
	movie->stop();
	//读取配置文件,设置。
 
	pSocket = new SocketRecvStates(this);
	m_result = false;
	readFile();
 
	connect(pSocket, SIGNAL(sigUpdateResult(QString)), this, SLOT(slotUpdateResult(QString)));
	connect(pSocket, SIGNAL(sigStartTimer(bool)), this, SLOT(slotStartTimer(bool)));
	connect(this, SIGNAL(sigSleep()), this, SLOT(slotSleep()));
	openMovFile();
}
 
OnTopBox::~OnTopBox()
{
 
}
 
void OnTopBox::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.setPen(Qt::NoPen);
	painter.setBrush(bgColor);
	painter.drawRoundedRect(ui.frame->rect(), 10, 10);
	painter.fillRect(ui.frame->rect(), bgColor);
}
          
void OnTopBox::readFile()
{
 
}
 
void OnTopBox::openMovFile()
{
	
}
 
void OnTopBox::initAnimalshow()
{
	show();
	activateWindow();
 
	QDesktopWidget* pDesktopWidget = QApplication::desktop();
	//获取可用桌面大小
	QRect deskRect = QApplication::desktop()->availableGeometry();
	//获取主屏幕分辨率
	QRect screenRect = QApplication::desktop()->screenGeometry();
	//获取屏幕数量
	int nScreenCount = QApplication::desktop()->screenCount();
	animalGroup = new QParallelAnimationGroup;
	QPropertyAnimation *animationShow = new QPropertyAnimation(this, "pos");
 
	animationShow->setDuration(500);//动画总时间
	QPoint point = pos();
	animationShow->setStartValue(QPoint(pDesktopWidget->width() - width(),-height()));//这个是窗体运动的初始位置
 	animationShow->setEndValue(QPoint(pDesktopWidget->width() - width(), 0));
	animationShow->setEasingCurve(QEasingCurve::OutQuad);
	int iCount = animalGroup->animationCount();
	for (int i=0;i<iCount;i++)
	{
		QPropertyAnimation *animal = static_cast<QPropertyAnimation*>(animalGroup->animationAt(i));
		animalGroup->removeAnimation(animal);
	}
	QPropertyAnimation * panimaRe = new QPropertyAnimation(this, "windowOpacity");
	panimaRe->setDuration(100);//动画总时间
	panimaRe->setStartValue(1);//开始不透明
	panimaRe->setEndValue(1);//接触时透明
	panimaRe->setEasingCurve(QEasingCurve::OutQuad);//渐变时间曲线
 
	animalGroup->addAnimation(animationShow);
	animalGroup->addAnimation(panimaRe);
	animalGroup->start();
}
 
void OnTopBox::initAnimalHide()
{                   
	
	animationHide = new QPropertyAnimation(this, "windowOpacity");
	connect(animationHide, SIGNAL(finished()), this, SLOT(animationHideFinished()));
	
	animationHide->setDuration(500);//动画总时间
	animationHide->setEasingCurve(QEasingCurve::OutQuad);//渐变时间曲线
	animationHide->setStartValue(1);//开始不透明
	animationHide->setEndValue(0);//接触时透明
	animationHide->start();
}
 
void OnTopBox::slotWaitingforResult()
{
	QTime currentTime = QTime::currentTime();
	int elapsed = current_time.msecsTo(currentTime);
 
	if (elapsed > 10)
	{
		if (m_result)
		{
			QTime currentTime1 = QTime::currentTime();
			int elapsedssssss = current_time.msecsTo(currentTime1);
			qDebug() << elapsedssssss;
			if (elapsedssssss > 2000)
			{
				m_timer->stop();
 
				initAnimalHide();
				movie->stop();
			}
			
		}
		else
		{
			m_timer->stop();
			initAnimalHide();
			movie->stop();
		}
			
 	}
	else
	{
		
	}
	
}
 
void OnTopBox::animationHideFinished()
{
 
	
}
 
void OnTopBox::slotUpdateResult(QString strmsg)
{
	ui.label_2->setText(strmsg);
	QApplication::processEvents();
	m_result = true;
 
}
 
void OnTopBox::slotSleep()
{
	
}
 
 
void OnTopBox::slotStartTimer(bool bopen)
{
	if (bopen)
	{
 
		m_result = false;
  		movie->start();
		if (animalGroup!=NULL)
		{
			animalGroup->stop();
		}
		initAnimalshow();
 
		
	}
	else {
// 		if (m_result)
// 		{
// 			
// 		}
// 		else
// 		{
// 			initAnimalHide();
// 			movie->stop();
// 			animationHide->start();
// 		}
	m_timer->start(50);
	 current_time = QTime::currentTime();
 
	}
}
 
void OnTopBox::mousePressEvent(QMouseEvent *event)
{
	m_startPos = event->pos();
	QPoint point = event->pos();
 
}
void OnTopBox::mouseMoveEvent(QMouseEvent *event)
{
	QPoint point = event->globalPos() - m_startPos;
	move(point);
}
 
void OnTopBox::mouseReleaseEvent(QMouseEvent *event)
{
 
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值