PyQT5 QFileDialog窗口标题在mac上不显示



我在Windows上制作了一个PyQt5应用程序,现在我想在Mac上使用该应用程序。该应用程序提示用户选择几个不同的文件。我使用QFileDialog窗口的标题让用户知道哪些文件是这样的:

instrument_file_raw=QFileDialog().getOpenFileName(self, "Select Instrument File","","Excel (*.xlsx)")
instrument_file_raw=str(instrument_file_raw[0])

if instrument_file_raw=="":
error_dialog = QErrorMessage(self)
error_dialog.showMessage('No filename entered')
return

run_list_file=QFileDialog().getOpenFileName(self, "Select Run List File","","Excel (*.xlsx)")
run_list_file=str(run_list_file[0])

if run_list_file=="":
error_dialog = QErrorMessage(self)
error_dialog.showMessage('No filename entered')
return

然而,当我在Mac上运行相同的代码时,当文件资源管理器打开时没有显示窗口标题。即使我用

手动设置窗口标题
instrument_file_window=QFileDialog()
instrument_file_window.setWindowTitle("Select Instrument File")
instrument_file_raw=instrument_file_window.getOpenFileName(self,"Select Instrument File","","Excel (*.xlsx)") 

有没有办法显示Mac上的窗口标题?我如何指示用户输入什么?

这是一个尚未修复的已知问题(见QTBUG-59805),与macOS从10.11版本(El Capitan)开始的更改有关。

如果需要显示标题,唯一的解决方案是使用非本地文件对话框选项。

path, filter = QtWidgets.QFileDialog.getOpenFileName(
self, "Select Instrument File", "", "Excel (*.xlsx)", 
options=QtWidgets.QFileDialog.DontUseNativeDialog
)

注意getOpenFileName,像其他类似的函数一样,是一个静态的函数:构造和配置一个新的文件对话框实例,然后返回它。事实上,如果你仔细看我上面的代码,我没有在QFileDialog后面使用任何括号。
除了参数中的选项之外,如果是本机创建的对话框,没有方法可以访问,并且对于非本机对话框只有有限的访问,但只能通过使用复杂的系统(如计时器检查顶级小部件),这也并不总是可靠的。

以上也意味着:

  1. 你不需要创建一个新的实例,因为它不会被使用;
  2. 出于同样的原因,设置任何属性(如窗口标题)绝对没有效果;

最新更新