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

来源:互联网 发布:改良圈算法 编辑:程序博客网 时间:2024/06/05 00:55

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

头文件:SplashScreenMng.h

#ifndef SPLASHSCREENMNG_H#define SPLASHSCREENMNG_Hclass 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 0
原创粉丝点击