编码进度条建议 [英] Coding progress bar advice
问题描述
在我的应用程序中,我正在解析一个 xml 文件并使用 xsd 架构验证内容.当 xml 文件变大时,解析文件并验证内容需要一些时间.在这种情况下,我希望使用进度条可视化解析和验证阶段.如何做到这一点?
In my application i am parsing an xml file and validating the contents with xsd schema. When xml file gets bigger it takes some time to parse file and validate the contents. In this case I want visualize parsing and validation phases with progress bar. How to do this?
注意:我在 C++ 中使用 Qt
Note: I am using Qt with C++
推荐答案
基本上你只需要创建一个 QProgressDialog
实例:
Basically you just create a QProgressDialog
instance:
QProgressDialog progress("Parsing...", "Abort", 0, numOperations, this);
progress.setWindowModality(Qt::WindowModal);
其中 numOperations
是在解析完成之前您需要做的全部事情.为此,您可能需要对数据进行第一次快速传递,您只需计算要解析的元素总数或类似的东西,并将此值设置为上一个示例中的最大值 numOperations
代码.然后您进行实际处理并定期调用 setValue:
where numOperations
is the full number of things you need to do before the parsing is done. For this, you probably need to make a first quick pass through the data where you simply count the total number of elements to parse or something similar, and set this value as the maximum value numOperations
in the previous example code. Then you make the actual processing pass and call setValue periodically:
progress.setValue(finishedOperations);
其中finishedOperations 是到目前为止解析的东西的数量.
where finishedOperations is the number of things parsed so far.
这是假设您想要最简单的解决方案,其中进度条显示为单独的模式对话框.如果您想让用户有机会中止该过程,您需要实现一个连接到 canceled()
信号.
This is assuming you want the simplest solution where the progress bar appears as a separate modal dialog. If you want to give the user the opportunity to abort the process, you need to implement a slot which you connect to the canceled()
signal.
如果您不想在模态对话框中显示进度条,您只需显示一个 QProgressBar
某处.它通过定期调用 setValue() 以类似的方式工作.
If you don't want the progress bar in a modal dialog, you just show a QProgressBar
somewhere. It works in a similar way by calling setValue() periodically.
这篇关于编码进度条建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!