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的评论

原文地址:https://www.cnblogs.com/findumars/p/5706013.html