简体   繁体   English

一种在Mac上强制关闭模态QFileDialog的方法

[英]A way to forcefully close modal QFileDialog on Mac

I have a Qt application which at some point calls QFileDialog::getOpenFileName . 我有一个Qt应用程序,它在某些时候调用QFileDialog::getOpenFileName However, at any point my application can get an event which will make this file dialog irrelevant, so I want to forcefully close it. 但是,在任何时候我的应用程序都可以获得一个事件,这将使该文件对话框无关紧要,所以我想强行关闭它。

This is my way of trying to close this dialog: 这是我尝试关闭此对话框的方法:

QWidget *modalWidget = QApplication::activeModalWidget();
if (modalWidget)
    modalWidget->close();

This works on Windows, but I get strange behavior on Mac. 这适用于Windows,但我在Mac上遇到了奇怪的行为。 Instead of closing, it hides the dialog instead. 它不是关闭,而是隐藏对话框。 Execution never leaves QFileDialog::getOpenFileName call and since it's a modal dialog, underlying app becomes permanently frozen. 执行永远不会离开QFileDialog::getOpenFileName调用,因为它是一个模态对话框,底层应用程序将永久冻结。

For the sake of closure, I ended up closing the dialog by calling native [NSApp abortModal] . 为了关闭,我最终通过调用native [NSApp abortModal]来关闭对话框。

Note that this workaround might not be needed if this issue was fixed in the recent Qt releases (haven't checked since Qt 5.8). 请注意,如果在最近的Qt版本中修复了此问题,则可能不需要此解决方法(自Qt 5.8以来未检查过)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM