【qt】程序忙碌时的进度条
来源:互联网 发布:电脑截动图用什么软件 编辑:程序博客网 时间:2024/05/22 04:44
当程序在执行一项(或多项)耗时比较久的操作时,界面总要有一点东西告诉用户“程序还在运行中”,那么,一个“没有终点”的进度条就是你需要的了。
PS:最好把耗时的操作扔到一个子线程中去,以免他阻塞了界面线程,造成程序卡死的假象。
reference:http://blog.csdn.net/small_qch/article/details/7664634
PS:最好把耗时的操作扔到一个子线程中去,以免他阻塞了界面线程,造成程序卡死的假象。
思路:程序很简单,一个进度条,一个定时器就足够了。
截图:
源代码:
- #include <QtCore>
- #include <QtGui>
- class WaitingDialog : public QDialog
- {
- Q_OBJECT
- private:
- int m_CurrentValue; //当前值
- int m_UpdateInterval; //更新间隔
- int m_MaxValue; //最大值
- QTimer m_Timer;
- QProgressBar *m_ProgressBar;
- public:
- WaitingDialog(QWidget *parent = 0);
- ~WaitingDialog();
- void Start(int interval=100, int maxValue=100);
- void Stop();
- private slots:
- void UpdateSlot();
- };
- WaitingDialog::WaitingDialog(QWidget *parent)
- {
- m_ProgressBar = new QProgressBar(this);
- m_CurrentValue = m_MaxValue = m_UpdateInterval = 0;
- m_ProgressBar->setRange(0, 100);
- connect(&m_Timer, SIGNAL(timeout()), this, SLOT(UpdateSlot()));
- m_ProgressBar->setTextVisible(false);
- QHBoxLayout *layout = new QHBoxLayout;
- layout->addWidget(m_ProgressBar);
- setLayout(layout);
- }
- WaitingDialog::~WaitingDialog()
- {
- }
- void WaitingDialog::UpdateSlot()
- {
- m_CurrentValue++;
- if( m_CurrentValue == m_MaxValue )
- m_CurrentValue = 0;
- m_ProgressBar->setValue(m_CurrentValue);
- }
- void WaitingDialog::Start(int interval/* =100 */, int maxValue/* =100 */)
- {
- m_UpdateInterval = interval;
- m_MaxValue = maxValue;
- m_Timer.start(m_UpdateInterval);
- m_ProgressBar->setRange(0, m_MaxValue);
- m_ProgressBar->setValue(0);
- }
- void WaitingDialog::Stop()
- {
- m_Timer.stop();
- }
- #include "main.moc"
- int main(int argc, char **argv)
- {
- QApplication app(argc, argv);
- WaitingDialog *dialog = new WaitingDialog;
- dialog->setWindowTitle("Please wait...");
- QEventLoop *loop = new QEventLoop;
- dialog->Start(50, 150),
- dialog->show();
- //开启一个事件循环,10秒后退出
- QTimer::singleShot(10000, loop, SLOT(quit()));
- loop->exec();
- return 0;
- }
阅读全文
0 0
- QT:程序忙碌时的进度条
- 【qt】程序忙碌时的进度条
- 程序忙碌时的进度条
- 程序进度条的实现
- linux的进度条程序
- Qt进度条
- QT进度条
- Qt进度条
- qt 进度条
- Qt进度条
- 忙碌的一天
- 忙碌的一月
- 忙碌的原因
- 忙碌的一个礼拜!
- 超级忙碌的一周
- 忙碌的一天
- 紧张忙碌的一天
- 忙碌的TA
- URL编码
- SpingMvc详解
- 463. Island Perimeter
- Spark2.0-RDD分区原理分析
- 栈的链接存储结构及实现
- 【qt】程序忙碌时的进度条
- C语言中指针和自增运算符结合时的运算顺序问题
- Python的基础语法
- mysql 数据统计 分区函数
- 面试题19:二叉树的镜像
- PLSQL developer 连接不上64位Oracle 的解决方法
- 2017年第0届浙江工业大学之江学院程序设计竞赛决赛—G(数论)
- Android Studio 出现 Gradle's dependency cache may be corrupt 错误解决办法
- android monkey测试