Qt QSplashScreen显示动画(gif效果)和消息

29 篇文章 8 订阅

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

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值