没有关闭按钮的QProgressDialog

5

是否可以移除 QProgressDialog 的关闭按钮(请参见屏幕截图)?我在文档/Google中没有找到有用的信息。

我使用模态 QProgressDialog 显示无限进程,并阻止GUI,直到长时间操作完成。因为GUI应该被阻塞,所以我不希望用户能够关闭对话框。

enter image description here


你是指取消按钮还是系统关闭按钮? - RobbieE
系统关闭按钮。 - sashoalm
https://dev59.com/mWkw5IYBdhLWcg3wVpFi - spiritwolfform
1个回答

11

通过清除相应的标志,您可以隐藏每个窗口的关闭按钮:

使用Qt 5.0

QProgressDialog dlg;
dlg.setWindowFlags(dlg.windowFlags() & ~Qt::WindowCloseButtonHint);

Qt::WindowCloseButtonHint 0x08000000 增加一个关闭按钮。在某些平台上,这意味着需要加入 Qt::WindowSystemMenuHint 才能起作用。

早期版本中

    QProgressDialog dlg;
    dlg.setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);

含义

  • Qt::Window 代表窗口
  • Qt::WindowTitleHint 代表在窗口顶部显示标题
  • Qt::CustomizeWindowHint 代表不显示按钮

嗯,它没有起作用。可能是因为我使用了 QProgressDialog::exec() 来显示对话框? - sashoalm
@sashoalm,在Windows中,按钮保持可见,但您无法按下它。您可能还应该隐藏帮助按钮以使其不可见。我也已经使用exec进行了测试。你的行为怎么样? - Lol4t0
当我按照您的代码粘贴时,关闭按钮会保留且不被禁用。当我去掉帮助按钮和关闭按钮的标志时,帮助按钮消失了,但关闭按钮仍然存在(且可被点击)。 - sashoalm
@sashoalm,您使用Linux吗?也许您的窗口管理器不支持此功能。您可以尝试使用标志进行操作。例如,dlg.setWindowFlags(Qt :: WindowTitleHint | Qt :: Window | Qt :: CustomizeWindowHint); 可以生成一个没有任何按钮的窗口,而 Qt :: Popup 可以生成没有边框的窗口。 - Lol4t0
1
不,我在使用Windows 7。你的电脑上可以运行吗?这段代码对我有效 - dlg.setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint | Qt::WindowTitleHint);,来自这个回答 - https://dev59.com/mWkw5IYBdhLWcg3wVpFi#15656707 - sashoalm
@sashoalm,我发现它能在Qt 5.0上运行但无法在Qt 4.8上运行。 - Lol4t0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接