I made a PyQt5 application on Windows and now I want to use the application on a Mac. The app prompts the user to select a couple different files. I used the title of the QFileDialog window to let the user know which files like so:
instrument_file_raw=QFileDialog().getOpenFileName(self, "Select Instrument File","","Excel (*.xlsx)") instrument_file_raw=str(instrument_file_raw) 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) if run_list_file=="": error_dialog = QErrorMessage(self) error_dialog.showMessage('No filename entered') return
However when I run the same code on Mac there is no window title shown when the file explorer opens. Even if I manually set the window title by
instrument_file_window=QFileDialog() instrument_file_window.setWindowTitle("Select Instrument File") instrument_file_raw=instrument_file_window.getOpenFileName(self,"Select Instrument File","","Excel (*.xlsx)")
Is there a way to show the window title on Mac? How can I indicate to the user what to input?
This is a known problem that has not been fixed yet (see QTBUG-59805) and related to a change to macOS starting from version 10.11 (El Capitan).
If you do need to display the caption, the only solution is to use the non native file dialog option.
path, filter = QtWidgets.QFileDialog.getOpenFileName( self, "Select Instrument File", "", "Excel (*.xlsx)", options=QtWidgets.QFileDialog.DontUseNativeDialog )
getOpenFileName, like other similar functions, is a static function: it constructs and configure a new file dialog instance and then returns it. In fact, if you closely look at my code above, I didn’t use any parentheses after
Besides the options in the arguments, there’s no way to access the created dialog if it’s native, and there is only limited access for non native dialogs, but only by using complex systems (like a timer that checks for the top level widget) which are also not always reliable.
The above also means that:
- you don’t need to create a new instance, since it won’t be used;
- for the same reason, setting any property (like the window title) has absolutely no effect;