QFileDialog::getOpenFileName Fails to Display Dialog with Native macOS
-
Hi Qt Devs! I'm developing a desktop application in C++ on macOS Sequoia 15.5 with Qt 6.9.1. When I call QFileDialog::getOpenFileName from a class that inherits from QWidget while on the main thread no dialog appears and the function immediately returns an empty string. When I call QFileDialog::getOpenFileName with the QFileDialog::DontUseNativeDialog option, however, I do indeed get a dialog and can retrieve a file name. I would like to use the native dialog, is there some reason the call is not displaying a file dialog with the native version?
In short, this code does not display a dialog on macOS:
QFileDialog::getOpenFileName(this, tr("Please select a file."));But this code does display a dialog on macOS:
QFileDialog::getOpenFileName(this, tr("Please select a file."), QString(), QString(), nullptr, QFileDialog::DontUseNativeDialog);Thanks!
Edit: The same problem occurs with QFileDialog::getSaveFileName and QFileDialog::getExistingDirectory too. Other calls like QMessageBox::information do work as expected, however.
-
Hi,
Please provide a complete minimal example that shows that issue. This will allow people to test in the same conditions as you.
-
Same issue here, when I press my browse button MacOS asks me for permission to access files on a removable volume, and when I click OK, the dialog just disappears and nothing happens. The browse dialog is not showing at all.
I'm on M4 Pro, macOS Tahoe, QT version is 6.9.2void MainWindow::onLoadModel() { QString path = QFileDialog::getOpenFileName(this, tr("Load Model"), QDir::homePath(), tr("Model Files (*.csv);;All Files (*)")); if (path.isEmpty()) return; if (worker) worker->setLoadPath(path); } -
Yes, I can confirm this issue as well on Qt 6.9.3 and MacOS 15.6.1
When passing QFileDialog::DontUseNativeDialog for options parameters the dialog shows correctly, e.g.:QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), lastOpenDir, tr("Images (*.png *.bmp *.jpg *.tif *.gif)"), nullptr, QFileDialog::DontUseNativeDialog);Note that this is a pointer to the QMainWindow of the application.
However I found this related bug report: QTBUG-135882
It has been closed, but when reading through the comments, apparently the solution is to replace add_executable with qt_add_executable in the CMakeLists.txt.
I tried this and it works for me. No need to pass the QFileDialog::DontUseNativeDialog parameter anymore and it will show the native MacOS dialog correctly.
-
Quoting the reasons from the ticket:
The app is missing a bundle identifier, which apparently affects whether the OS shows native file dialogs. Set MACOSX_BUNDLE_GUI_IDENTIFIER to fix this. The reason qt_add_executable works is because it automatically sets MACOSX_BUNDLE_GUI_IDENTIFIER if not set.