QT:如何创建一个简单的 QProgressDialog 对象

发布于 2025-01-10 16:10:31 字数 1269 浏览 2 评论 0原文

我想向 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 项目的其他示例中,我注意到传递了 parentnullptrthis 对象。 为什么它不能与 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.
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

层林尽染 2025-01-17 16:10:31

我终于把一切都弄清楚了。以下是我的发现:

  • QProgressDialog 可以在没有父级的情况下使用
  • 仅当 progress.setMinimumDuration( 0 ) 后面跟着 progress 时,黑色小部件才会填充内容。 setValue(...) 调用后跟 QApplication::processEvents() 调用。这可能是因为只有 progress.setValue() 触发小部件为输出做好准备,然后 QApplication::processEvents() 触发最终输出:
    progress.setMinimumDuration( 0 );
    progress.setValue(...);
    QApplication::processEvents();
  • 要将QProgressDialog与QGIS主窗口作为父窗口一起使用,可以通过qApp->topLevelWidgets()访问小部件对象名称“QgisApp”(示例取自QgsGml::getFeatures())

    //查看是否有QGIS主窗口。如果是,则显示进度对话框
    QProgressDialog *progressDialog = nullptr;
    QWidget *mainWindow = nullptr;
    const QWidgetList topLevelWidgets = qApp->topLevelWidgets();
    for ( QWidgetList::const_iterator it = topLevelWidgets.constBegin(); it != topLevelWidgets.constEnd(); ++it )
    {
      if ( ( *it )->objectName() == QLatin1String( "QgisApp" ) )
      {
        主窗口=*它;
        休息;
      }
    }
    if ( 主窗口 )
    {
      进度对话框 = new QProgressDialog( ... );
      ...
    }
    

更新

  • 如果再次设置初始最小值,进度条将保持黑色,例如
    progressDialog = new QProgressDialog("Calculating .."), "Abort", 0, max);
    progress.setValue(0);
    QApplication::processEvents();
  • progressDialog->show()即使通过 progressDialog->setMinimumDuration( 2000 ) 设置了最短持续时间,也会引发窗口,但
  • 在最小持续时间后窗口将保持黑色,必须按顺序设置进度值 显示的进度窗口
  • 在设置值之前调用 QApplication::processEvents()
  • 窗口显示为黑色 A progressDialog->setValue(...) 显示为黑色,直到后面出现 QApplication::processEvents()

I finally found everything out. These are my findings:

  • QProgressDialog can be used without a parent
  • The black widget is filled with content only when progress.setMinimumDuration( 0 ) is followed by a progress.setValue(...) call followed by a QApplication::processEvents() call. This is probably because only the progress.setValue() triggers the widget to be prepared for the output and the QApplication::processEvents() then triggers the final output:
    progress.setMinimumDuration( 0 );
    progress.setValue(...);
    QApplication::processEvents();
  • To use the QProgressDialog with the QGIS main window as parent the widget objectName "QgisApp" can be accessed via qApp->topLevelWidgets() (example taken from QgsGml::getFeatures())

    //find out if there is a QGIS main window. If yes, display a progress dialog
    QProgressDialog *progressDialog = nullptr;
    QWidget *mainWindow = nullptr;
    const QWidgetList topLevelWidgets = qApp->topLevelWidgets();
    for ( QWidgetList::const_iterator it = topLevelWidgets.constBegin(); it != topLevelWidgets.constEnd(); ++it )
    {
      if ( ( *it )->objectName() == QLatin1String( "QgisApp" ) )
      {
        mainWindow = *it;
        break;
      }
    }
    if ( mainWindow )
    {
      progressDialog = new QProgressDialog( ... );
      ...
    }
    

UPDATE:

  • A progress bar keeps black if an initial min value is set again, e.g.
    progressDialog = new QProgressDialog("Calculating .."), "Abort", 0, max);
    progress.setValue(0);
    QApplication::processEvents();
  • progressDialog->show() raises the window even if a minimum duration is set via progressDialog->setMinimumDuration( 2000 ) but the window stays black
  • after the minimumDuration time the progress value must be set in order the progress window to appear
  • when QApplication::processEvents() is called before a value is set the window appears black
  • A progressDialog->setValue(...) is shown black until followed by QApplication::processEvents()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文