QT程序啟動畫面問題


程序啟動時比較枯燥,同時為了增加程序趣味,考慮做一個啟動畫面。

在Qt中實現啟動界面,主要就是使用QSplashScreen類。關於QSplashScreen類的使用問題,主要參考

http://blog.csdn.net/chenlong12580/article/details/23713025

http://blog.csdn.net/andy_93/article/details/52700697

然而此博客僅僅介紹了如何封裝QSplashScreen類,並為介紹怎么使用,而我僅僅是一個QT新手,在使用過程中遇到一些問題,特此記錄

首先是頭文件SplashScreenMng.h的封裝,實現如下

 1 #ifndef SPLASHSCREENMNG_H  
 2 #define SPLASHSCREENMNG_H  
 3   
 4 class QMutex;  
 5 class QSplashScreen;  
 6 class QString;  
 7   
 8 class CSplashScreenMng  
 9 {  
10 public:  
11     /** 
12      * 函數功能:獲取CSplashScreenMng的唯一對象指針 
13      * 返回值: CSplashScreenMng對象指針 
14      */  
15     static CSplashScreenMng* getInstance();  
16       
17     /** 
18      * 函數功能:獲取QSplashScreen指針對象並顯示 
19      * 參數:  fileName [in] QSplashScreen顯示的圖片文件 
20      * 返回值:QSplashScreen對象指針 
21      */  
22     QSplashScreen* showSplash(const QString &fileName);  
23       
24     /** 
25      *  函數功能:設置QSplashScreen對象的內容 
26      *  參數: text [in] 需要顯示的內容 
27      */  
28     void setSplashStatus(const QString &text);  
29       
30     //銷毀QSplashScreen對象  
31     void destroySplash();  
32 private:  
33     CSplashScreenMng();  //構造函數  
34     ~CSplashScreenMng(); //析構函數  
35     CSplashScreenMng(const CSplashScreenMng &other); //拷貝構造  
36     CSplashScreenMng& operator=(const CSplashScreenMng &other); //賦值運算操作符  
37       
38 private:  
39     QSplashScreen*  m_pSplash;   
40     static QMutex mutex;  
41     static CSplashScreenMng* m_pSplashScreenMng; // CSplashScreenMng 全局唯一的變量  
42 };  
43   
44 #endif //SPLASHSCREENMNG_H  

其次是關於實現文件SplashScreenMng.cpp

 1 #include "SplashScreenMng.h"  
 2 #include <QMutex>  
 3 #include <QObject>  
 4 #include <QPixmap>  
 5 #include <QSplashScreen>  
 6 #include <QString>  
 7   
 8 QMutex CSplashScreenMng::mutex;  
 9 CSplashScreenMng* CSplashScreenMng::m_pSplashScreenMng = NULL;  
10   
11 CSplashScreenMng::CSplashScreenMng()  
12 {  
13     m_pSplash = NULL;  
14 }  
15   
16 CSplashScreenMng::~CSplashScreenMng()  
17 {  
18   
19 }  
20   
21 CSplashScreenMng* CSplashScreenMng::getInstance()  
22 {  
23     if(NULL == m_pSplashScreenMng){  
24         mutex.lock();  
25         if(NULL == m_pSplashScreenMng){  
26             m_pSplashScreenMng = new CSplashScreenMng();  
27         }  
28         mutex.unlock();  
29     }  
30     return m_pSplashScreenMng;  
31 }  
32       
33 QSplashScreen*  CSplashScreenMng::showSplash(const QString &fileName)  
34 {  
35     QPixmap pixmap(fileName);  
36     if(pixmap.isNull())  
37         return NULL;  
38   
39     m_pSplash = new QSplashScreen(pixmap);  
40     m_pSplash->show();  
41     setSplashStatus(QObject::tr("初始化..."));  
42       
43     return  m_pSplash;  
44 }  
45       
46 void CSplashScreenMng::setSplashStatus(const QString &text)  
47 {  
48     if(!m_pSplash)  
49         return ;  
50   
51     QString splashText = text;  
52     splashText += QObject::tr("請稍后...");  
53     m_pSplash->showMessage(splashText, Qt::AlignLeft | Qt::AlignBottom, Qt::white);//這里設置為左下角顯示信息  
54 }  
55   
56 void CSplashScreenMng::destroySplash()  
57 {  
58     if(m_pSplash){  
59         delete m_pSplash;  
60         m_pSplash = NULL;  
61     }  
62 }  

文件封裝結束需要調用,在調用過程中,資源文件的配置上圖片格式無強制要求。但是在使用中僅僅放到工程目錄Resources下程序仍然無法識別。后來嘗試發現,是沒有放入資源文件中。QT的資源文件是*.qrc,但是資源文件在VS2013環境下無法直接打開。這時可以通過打開Form Files下的*.ui,然后在QtDesigner下編輯*.qrc,增加圖片文件,然后調用即可。

調用時的代碼如下

 1 #include <windowsx.h>
 2 #include<QSplashScreen>
 3 #include<QtTest/qtest.h>
 4 int main(int argc, char *argv[])
 5 {
 6     QApplication a(argc, argv);
 7     
 8     QSplashScreen* splash =CSplashScreenMng::getInstance()->showSplash(":/TunnelView/Resources/Initialize3.jpg");  
9
//CSplashStart::getInstance()->setSplashStatus("Welcome");

10   QTest::qSleep(1000);//延時

11   a.processEvents();
12   TunnelView w;
13   w.show();
14   splash->finish(&w);
15   CSplashScreenMng::getInstance()->DestroySplash();
16   return a.exec();
17 }

在選擇圖片目錄時圖片目錄要包含至工程Resources上一層目錄,否則圖片無法加載。此時采用在QtDesigner中的資源管理中復制目錄最方便,避免出錯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM