QT:如何创建一个简单的 QProgressDialog 对象
我想向 qgsogrprovider.cpp 可以有更长的时间计算次数。
当我尝试通过添加来显示此进度对话框小部件时 #include
到
QProgressDialog progress(tr("Calculating extent.."), QString(), 0, 100);
progress.setWindowModality(Qt::WindowModal);
progress.setValue(30);
progress.setWindowTitle( tr( "Progress Indication" ) );
progress.setAutoClose( true );
progress.setMinimumDuration( 0 );
progress.setLabelText("Labeltext");
它编译和链接的包含项和函数,没有任何问题,但在执行代码时,当应该显示进度对话框时,我只得到一个黑框。
根据QT QDialog描述和QObject 描述 我知道不一定需要提供父小部件,但是这样做会更好,因为小部件将位于父部件的中心,并且在完成后它会自动销毁。 在 QGIS 项目的其他示例中,我注意到传递了 parent
、nullptr
或 this
对象。 为什么它不能与 nullptr
一起使用,以及如何找到父级或创建可以在必要时传递给函数的父级?
I want to add a simple QProgressDialog
progress dialog to a function called QgsOgrProvider::extent()
in qgsogrprovider.cpp of the QGIS project that can have longer calculation times.
When I tried to get this progress dialog widget on display by adding#include <QProgressDialog>
to the includes and
QProgressDialog progress(tr("Calculating extent.."), QString(), 0, 100);
progress.setWindowModality(Qt::WindowModal);
progress.setValue(30);
progress.setWindowTitle( tr( "Progress Indication" ) );
progress.setAutoClose( true );
progress.setMinimumDuration( 0 );
progress.setLabelText("Labeltext");
to the function it compiles and links without any problem but when executing the code I only get a black box at the moment when the progress dialog should be shown.
According to the QT QDialog description and QObject description I understand that no parent widget necessarily needs to be provided but it would be better to do so as the widget will be positioned in the center to the parent and also it will be destroyed automatically after having finished.
In other examples of the QGIS project I have noticed that either a parent
is passed, a nullptr
or a this
object.
Why does it not work with a nullptr
and how do I find a parent or create a parent that can be passed to the function if necessary?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于把一切都弄清楚了。以下是我的发现:
QProgressDialog
可以在没有父级的情况下使用progress.setMinimumDuration( 0 )
后面跟着progress 时,黑色小部件才会填充内容。 setValue(...)
调用后跟QApplication::processEvents()
调用。这可能是因为只有progress.setValue()
触发小部件为输出做好准备,然后QApplication::processEvents()
触发最终输出:要将
QProgressDialog
与QGIS主窗口作为父窗口一起使用,可以通过qApp->topLevelWidgets()
访问小部件对象名称“QgisApp”(示例取自QgsGml::getFeatures()
)更新:
progressDialog->show()
即使通过progressDialog->setMinimumDuration( 2000 )
设置了最短持续时间,也会引发窗口,但QApplication::processEvents()
时progressDialog->setValue(...)
显示为黑色,直到后面出现QApplication::processEvents()
I finally found everything out. These are my findings:
QProgressDialog
can be used without a parentprogress.setMinimumDuration( 0 )
is followed by aprogress.setValue(...)
call followed by aQApplication::processEvents()
call. This is probably because only theprogress.setValue()
triggers the widget to be prepared for the output and theQApplication::processEvents()
then triggers the final output:To use the
QProgressDialog
with the QGIS main window as parent the widget objectName "QgisApp" can be accessed viaqApp->topLevelWidgets()
(example taken fromQgsGml::getFeatures()
)UPDATE:
progressDialog->show()
raises the window even if a minimum duration is set viaprogressDialog->setMinimumDuration( 2000 )
but the window stays blackQApplication::processEvents()
is called before a value is set the window appears blackprogressDialog->setValue(...)
is shown black until followed byQApplication::processEvents()