Details
-
Bug
-
Resolution: Unresolved
-
P2: Important
-
None
-
5.14.2, 5.15.1
-
None
Description
Description
When you show a modeless QFileDialog with a specified start directory on macOS, the QFileDialog doesn't display the content of the specified directory but fallbacks to a default folder.
Reproducibility
This issue is reproducible with:
- Qt 5.14.2 on macOS 10.15.7
- Qt 5.15.1 on macOS 10.15.7
Steps to reproduce
1. Download the attached 'OpenDialog' sample project
2. Compile and run with Qt 5.15.1 on macOS 10.15.7
3. Click on the 'ShowModelessPanel' button
Result: The QFileDialog displays the content of ~/Documents although /Library/ is specified as start directory.
More info
The sample code simply calls these 2 lines:
QFileDialog *fileDialogOpenSelection = new QFileDialog(this, QString(), QString("/Library/")); fileDialogOpenSelection->show();
The show() function will call the internal Qt function - (void)showModelessPanel in qtbase/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm :
- (void)showModelessPanel { if (mOpenPanel){ QFileInfo info(*mCurrentSelection); NSString *filepath = info.filePath().toNSString(); NSURL *url = [NSURL fileURLWithPath:filepath isDirectory:info.isDir()]; bool selectable = (mOptions->acceptMode() == QFileDialogOptions::AcceptSave) || [self panel:mOpenPanel shouldEnableURL:url]; [self updateProperties]; [mSavePanel setNameFieldStringValue:selectable ? info.fileName().toNSString() : @""]; [mOpenPanel beginWithCompletionHandler:^(NSInteger result){ mReturnCode = result; if (mHelper) mHelper->QNSOpenSavePanelDelegate_panelClosed(result == NSModalResponseOK); }]; } }
Note that the directory is not set in this function. After the [self updateProperties]; line, I would expect to see a line like:
[mSavePanel setDirectoryURL: [NSURL fileURLWithPath:mCurrentDir]];
Workarounds
1. The bug doesn't occur when you use QFileDialog as a sheet, i.e. by using open() instead of show():
QFileDialog *fileDialogOpenSelection = new QFileDialog(this, QString(), QString("/Library/")); fileDialogOpenSelection->open();
The reason is that the internal Qt function - (void)showWindowModalSheet:(QWindow *)parent in qtbase/src/plugins/platforms/cocoa/qcocoafiledialoghelper.mm properly sets the directory:
- (void)showWindowModalSheet:(QWindow *)parent { [...] [self updateProperties]; [mSavePanel setDirectoryURL: [NSURL fileURLWithPath:mCurrentDir]];
2. Before using QFileDialog, you could change the pref "NSNavLastRootDirectory"
Since QFileDialog doesn't set the start directory, macOS displays the content of the directory remembered in the preference "NSNavLastRootDirectory". So you could:
- remember the previous "NSNavLastRootDirectory" using QSettings
- set "NSNavLastRootDirectory" using QSettings to the wanted directory
- create the QFileDialog
- restore "NSNavLastRootDirectory" using QSettings to the previous value