zoukankan      html  css  js  c++  java
  • QT:程序忙碌时的进度条——开启时间循环,等结束的时候再退出

    当程序在执行一项(或多项)耗时比较久的操作时,界面总要有一点东西告诉用户“程序还在运行中”,那么,一个“没有终点”的进度条就是你需要的了。
    PS:最好把耗时的操作扔到一个子线程中去,以免他阻塞了界面线程,造成程序卡死的假象。

    思路:程序很简单,一个进度条,一个定时器就足够了。

    截图:

    源代码:

      1. #include <QtCore>  
      2. #include <QtGui>  
      3.   
      4. class WaitingDialog : public QDialog  
      5. {  
      6.     Q_OBJECT  
      7. private:  
      8.     int m_CurrentValue;         //当前值  
      9.     int m_UpdateInterval;       //更新间隔  
      10.     int m_MaxValue;             //最大值  
      11.     QTimer m_Timer;  
      12.     QProgressBar *m_ProgressBar;  
      13. public:  
      14.     WaitingDialog(QWidget *parent = 0);  
      15.     ~WaitingDialog();  
      16.     void Start(int interval=100, int maxValue=100);  
      17.     void Stop();  
      18.     private slots:  
      19.         void UpdateSlot();  
      20. };  
      21.   
      22. WaitingDialog::WaitingDialog(QWidget *parent)  
      23. {  
      24.     m_ProgressBar = new QProgressBar(this);  
      25.     m_CurrentValue = m_MaxValue = m_UpdateInterval = 0;  
      26.     m_ProgressBar->setRange(0, 100);  
      27.     connect(&m_Timer, SIGNAL(timeout()), this, SLOT(UpdateSlot()));  
      28.     m_ProgressBar->setTextVisible(false);  
      29.     QHBoxLayout *layout = new QHBoxLayout;  
      30.     layout->addWidget(m_ProgressBar);  
      31.     setLayout(layout);  
      32. }  
      33.   
      34. WaitingDialog::~WaitingDialog()  
      35. {  
      36.   
      37. }  
      38.   
      39. void WaitingDialog::UpdateSlot()  
      40. {  
      41.     m_CurrentValue++;  
      42.     if( m_CurrentValue == m_MaxValue )  
      43.         m_CurrentValue = 0;  
      44.     m_ProgressBar->setValue(m_CurrentValue);  
      45. }  
      46.   
      47. void WaitingDialog::Start(int interval/* =100 */, int maxValue/* =100 */)  
      48. {  
      49.     m_UpdateInterval = interval;  
      50.     m_MaxValue = maxValue;  
      51.     m_Timer.start(m_UpdateInterval);  
      52.     m_ProgressBar->setRange(0, m_MaxValue);  
      53.     m_ProgressBar->setValue(0);  
      54. }  
      55.   
      56. void WaitingDialog::Stop()  
      57. {  
      58.     m_Timer.stop();  
      59. }  
      60.   
      61. #include "main.moc"  
      62.   
      63. int main(int argc, char **argv)  
      64. {  
      65.     QApplication app(argc, argv);  
      66.     WaitingDialog *dialog = new WaitingDialog;  
      67.     dialog->setWindowTitle("Please wait...");  
      68.     QEventLoop *loop = new QEventLoop;  
      69.     dialog->Start(50, 150),   
      70.     dialog->show();  
      71.     //开启一个事件循环,10秒后退出  
      72.     QTimer::singleShot(10000, loop, SLOT(quit()));  
      73.     loop->exec();  
      74.     return 0;  
      75. }  

    http://blog.csdn.net/small_qch/article/details/7664634

  • 相关阅读:
    IOS开发mapkit学习笔记
    IOS的两种框架设计思想
    关于创建及使用多线程的几种方法
    Linux基础入门 之挑战:历史命令
    20172018网络攻防第三周
    20172018网络攻防第二周
    Linux基础入门实验楼挑战之数据提取
    iconfont字体图标的使用方法超简单!
    CSS中的绝对定位(absolute)误区
    获取对象属性的点方法和中括号法的区别
  • 原文地址:https://www.cnblogs.com/findumars/p/4993549.html
Copyright © 2011-2022 走看看