1. 程式人生 > >Qt開場動畫(gif效果)的實現

Qt開場動畫(gif效果)的實現

 

 

Qt自己提供了一個開場動畫的類QSplashScreen,可以實現簡單的圖片開場的效果,但是是靜態的圖片。

Qt播放gif格式圖片是利用的QMovie實現的。因此利用QMoviee和QTimer,每隔一段時間將QSplashScreen重繪一次,來實現gif動圖的效果。

具體使用:

CSplashScreen *splashscream = new CSplashScreen(":/inputdlg/2.gif");
 splashscream->show();

標頭檔案程式碼

#ifndef _SPLASHSCREEN_H_
#define _SPLASHSCREEN_H_

#include <QSplashScreen>
#include <QMovie>

class QPixmap;
class QPainter;

class CSplashScreen : public QSplashScreen
{
	Q_OBJECT
public:
	CSplashScreen(const QPixmap & pixmap);
	CSplashScreen(const QString gifname);

	~CSplashScreen();
	void setGif(QString filename);
	
protected:
	virtual void drawContents(QPainter *painter);

private slots:
	void slot_update();
	
private:
	QMovie *m_move;
	int m_rotate;
	
};

#endif // _SPLASHSCREEN_H_

cpp檔案程式碼

#include <QPixmap>
#include <QPainter>
#include "splashscreen.h"
#include <QLabel>

#include <QTimer>

CSplashScreen::CSplashScreen(const QPixmap & pixmap) : QSplashScreen(pixmap)
{
	
}

CSplashScreen::CSplashScreen( const QString gifname )
{
	setGif(gifname);
}

CSplashScreen::~CSplashScreen()
{

}

void CSplashScreen::slot_update()
{
	setPixmap(m_move->currentPixmap());
	repaint();
}

void CSplashScreen::drawContents(QPainter *painter)
{
	painter->setFont(QFont("SimHei", 40));
	painter->setPen(QColor(213, 218, 220));
	painter->drawText(QPointF(20, 100), "教員系統");

	painter->setFont(QFont("SimHei", 18));
	painter->setPen(QColor(213, 218, 220));
	painter->drawText(QPointF(30, 140), "Version: 1.0.0");

	painter->setFont(QFont("Helvetica", 16));
	painter->setPen(QColor(Qt::white));
	QRect r = rect();
	r.setRect(r.x(), r.y(), r.width(), r.height() -12);
	painter->drawText(r, Qt::AlignBottom | Qt::AlignCenter, "測試");

	painter->setFont(QFont("Verdana", 11));
	QSplashScreen::drawContents(painter);
}

void CSplashScreen::setGif( QString filename )
{
	m_move = new QMovie(filename);
	m_move->start();

	QTimer *timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(slot_update()));
	timer->start(100);
}