Qt帶進度條的啟動界面(繼承QSplashScreen,然后使用定時器)


        通過繼承QSplashScreen類,得到CMySplashScreen類,然后在CMySplashScreen中定義QProgressBar變量,該變量以CMySplashScreen為父類,這樣就實現了帶進度條的啟動界面。

        進度條加入后,需要控制進度條的值,為了讓進度條看起來更逼真,可以通過生成隨機數的方式,把隨機數當做進度條的值。此時,生成的隨機數必須是從小到大排列的,要不然進度條就不會從開端一步步走到終點,而是會出現走到一半后又回到開端等反常現象。如何生成隨機數可參考http://blog.csdn.net/caoshangpa/article/details/51036267

        CMySplashScreen類的實現如下所示。

 

[cpp]  view plain  copy
 
  1. #ifndef CMYSPLASHSCREEN_H  
  2. #define CMYSPLASHSCREEN_H  
  3.   
  4. #include <QSplashScreen>  
  5. #include <QPixmap>  
  6. #include <QProgressBar>  
  7. #include <QList>  
  8. #include <QtGlobal>  
  9.   
  10. class CMySplashScreen: public QSplashScreen  
  11. {  
  12.      Q_OBJECT  
  13.   
  14. public:  
  15.      CMySplashScreen(QPixmap& pixmap,int time);  
  16.      ~CMySplashScreen();  
  17.   
  18. private:  
  19.      //進度條  
  20.      QProgressBar *ProgressBar;  
  21.      //隨機數列表  
  22.      QList<int> numbersList;  
  23.      //啟動界面停留的時間  
  24.      int elapseTime;  
  25.   
  26. private:  
  27.      void setProgress();  
  28.      void generateAscendRandomNumber();  
  29.   
  30. private slots:      
  31.      void slotUpdateProgress();  
  32. };  
  33.   
  34. #endif // CMYSPLASHSCREEN_H  

 

 

[cpp]  view plain  copy
 
  1. #include "cmysplashscreen.h"  
  2. #include <QTime>  
  3. #include <QTimer>  
  4. CMySplashScreen::CMySplashScreen(QPixmap& pixmap,int time) :  
  5.     QSplashScreen(pixmap),  
  6.     elapseTime(time)  
  7. {  
  8.     ProgressBar = new QProgressBar(this);  
  9.     //設置進度條的位置  
  10.     ProgressBar->setGeometry(0,pixmap.height()-50,pixmap.width(),30);  
  11.     //設置進度條的樣式  
  12.     ProgressBar->setStyleSheet("QProgressBar {color:black;font:30px;text-align:center; }QProgressBar::chunk {background-color: rgb(202, 165, 14);}");  
  13.     //設置進度條的樣式  
  14.     ProgressBar->setRange(0, 100);  
  15.     //設置進度條的當前進度  
  16.     ProgressBar->setValue(0);  
  17.   
  18.     generateAscendRandomNumber();  
  19.     setProgress();  
  20. }  
  21.   
  22. CMySplashScreen::~CMySplashScreen()  
  23. {  
  24.   
  25. }  
  26.   
  27. void CMySplashScreen::setProgress()  
  28. {  
  29.     int tempTime=elapseTime/100;  
  30.     for(int i=0;i<100;i++)  
  31.     {  
  32.        QTimer::singleShot(i*tempTime, this, SLOT(slotUpdateProgress()));  
  33.     }  
  34.     QTimer::singleShot(elapseTime, this, SLOT(close()));  
  35. }  
  36.   
  37. void CMySplashScreen::slotUpdateProgress()  
  38. {  
  39.     static int num=0;  
  40.     ProgressBar->setValue(numbersList[num]);  
  41.     num++;  
  42. }  
  43.   
  44. void CMySplashScreen::generateAscendRandomNumber()  
  45. {  
  46.     int i;  
  47.     qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));  
  48.     //生成100個大小在[0,100]之間的隨機數  
  49.     for(i=0;i<100;i++)  
  50.     {  
  51.         numbersList.append(qrand()%101);  
  52.     }  
  53.     //遞增排序  
  54.     qSort(numbersList.begin(),numbersList.end());  
  55. }  


啟動界面效果如下所示。


源碼鏈接:見http://blog.csdn.net/caoshangpa/article/details/51037427的評論

 


免責聲明!

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



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