gpt4 book ai didi

qt - QProgressBar 忙不工作

转载 作者:行者123 更新时间:2023-12-05 05:20:30 26 4
gpt4 key购买 nike

我正在尝试创建一个 QProgressBar 来指示执行一个我不知道执行前需要多长时间才能完成的操作。

现在我有以下内容

QProgressBar DLbar;
DLbar.setMaximum(0);
DLbar.setMinimum(0);
DLbar.show();

我已将最小值和最大值都设置为 0,这将导致基于 Qt documentation 的忙碌指示器:

If minimum and maximum both are set to 0, the bar shows a busy indicator instead of a percentage of steps. This is useful, for example, when using QNetworkAccessManager to download items when they are unable to determine the size of the item being downloaded.

当我运行该程序时,会显示进度条,但它不会显示忙碌指示器,而是已满并保持这种状态,直到操作完成。

我尝试将 DLbar 父窗口设置为主窗口,但存在同样的问题。

例子:

QProgressBar DLbar;
DLbar.setMaximum(0);
DLbar.setMinimum(0);
DLbar.show();
for( int i=0; i<1000000; i++ )
qDebug() << i;

最佳答案

基本问题是您不允许 Qt 事件循环处理事件。一个最小的例子来证明是......

#include <cstdlib>
#include <chrono>
#include <thread>
#include <QApplication>
#include <QProgressBar>

int
main (int argc, char **argv)
{
QApplication app(argc, argv);
QProgressBar DLbar;
DLbar.setMaximum(0);
DLbar.setMinimum(0);
DLbar.show();

/*
* The following sleep will prevent any events being processed for 10s...
*/
std::this_thread::sleep_for(std::chrono::seconds(10));

/*
* ...after which we start the event loop and everything should start working.
*/
exit(app.exec());
}

如果您要使用 Qt,您确实需要深入了解底层 event system .

编辑 1: 你问...

i still don't understand why it's not working even if i use QCoreApplication::processEvents() inside the loop.

如果在上面的示例中,您替换...,它将起作用

exit(app.exec());

与...

while (true)
QCoreApplication::processEvents();

关于qt - QProgressBar 忙不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44568097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com