Qt利用QSplashScreen类自定义初始化启动界面

28 篇文章 2 订阅

前段时间在做Qt项目的时候需要做一个程序启动画面,于是自己定义了一个类来实现自定义启动界面。写得不好的地方,望大家指出!

头文件:SplashScreenMng.h

#ifndef SPLASHSCREENMNG_H
#define SPLASHSCREENMNG_H

class QMutex;
class QSplashScreen;
class QString;

class CSplashScreenMng
{
public:
	/**
	 * 函数功能:获取CSplashScreenMng的唯一对象指针
	 * 返回值:	CSplashScreenMng对象指针
	 */
	static CSplashScreenMng* getInstance();
	
	/**
	 * 函数功能:获取QSplashScreen指针对象并显示
	 * 参数:	fileName [in] QSplashScreen显示的图片文件
	 * 返回值:QSplashScreen对象指针
	 */
	QSplashScreen* showSplash(const QString &fileName);
	
	/**
	 *  函数功能:设置QSplashScreen对象的内容
	 *	参数: text [in] 需要显示的内容
	 */
	void setSplashStatus(const QString &text);
	
	//销毁QSplashScreen对象
	void destroySplash();
private:
	CSplashScreenMng();  //构造函数
	~CSplashScreenMng(); //析构函数
	CSplashScreenMng(const CSplashScreenMng &other); //拷贝构造
	CSplashScreenMng& operator=(const CSplashScreenMng &other); //赋值运算操作符
	
private:
	QSplashScreen*	m_pSplash; 
	static QMutex mutex;
	static CSplashScreenMng* m_pSplashScreenMng; // CSplashScreenMng 全局唯一的变量
};

#endif //SPLASHSCREENMNG_H

</pre><pre>
源文件:SplashScreenMng.cpp
</pre><pre>

#include "SplashScreenMng.h"
#include <QMutex>
#include <QObject>
#include <QPixmap>
#include <QSplashScreen>
#include <QString>

QMutex CSplashScreenMng::mutex;
CSplashScreenMng* CSplashScreenMng::m_pSplashScreenMng = NULL;

CSplashScreenMng::CSplashScreenMng()
{
    m_pSplash = NULL;
}

CSplashScreenMng::~CSplashScreenMng()
{

}

CSplashScreenMng* CSplashScreenMng::getInstance()
{
	if(NULL == m_pSplashScreenMng){
	    mutex.lock();
		if(NULL == m_pSplashScreenMng){
			m_pSplashScreenMng = new CSplashScreenMng();
		}
		mutex.unlock();
	}
	return m_pSplashScreenMng;
}
	
QSplashScreen*	CSplashScreenMng::showSplash(const QString &fileName)
{
	QPixmap pixmap(fileName);
	if(pixmap.isNull())
		return NULL;

	m_pSplash = new QSplashScreen(pixmap);
	m_pSplash->show();
	setSplashStatus(QObject::tr("初始化..."));
	
	return  m_pSplash;
}
	
void CSplashScreenMng::setSplashStatus(const QString &text)
{
	if(!m_pSplash)
		return ;

	QString splashText = text;
	splashText += QObject::tr("请稍后...");
	m_pSplash->showMessage(splashText, Qt::AlignLeft | Qt::AlignBottom, Qt::white);//这里设置为左下角显示信息
}

void CSplashScreenMng::destroySplash()
{
	if(m_pSplash){
		delete m_pSplash;
		m_pSplash = NULL;
	}
}


体使用:

1 获取QSplashScreen 指针 

      QSplashScreen* splash = CSplashScreenMng::getInstance()->showSplash("start.bmp");//此处需要传递资源文件

2 当获取指针成功后 可以通过调用 

        CSplashScreenMng::getInstance()->setSplashStatus(str); //传递需要显示的内容

3 在初始化完成后使用splash ->finish()并且调用 

        CSplashScreenMng::getInstance()->destroySplash()函数释放内存即可


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值