Qt开场动画(gif效果)的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chyuanrufeng/article/details/82980978

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);
}


猜你喜欢

转载自blog.csdn.net/chyuanrufeng/article/details/82980978
今日推荐