Bug 507825

Summary: The File dialog of KUrlRequester opens an incorrect location
Product: [Frameworks and Libraries] frameworks-kio Reporter: Jean-Baptiste Mardelle <jb>
Component: generalAssignee: KIO Bugs <kio-bugs-null>
Status: REPORTED ---    
Severity: normal CC: kdelibs-bugs-null, nicolas.fella
Priority: NOR    
Version First Reported In: 6.16.0   
Target Milestone: ---   
Platform: Microsoft Windows   
OS: Microsoft Windows   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Jean-Baptiste Mardelle 2025-08-03 19:14:29 UTC
This is a Windows specific issue, probably related to the fact that it uses native file dialogs

STEPS TO REPRODUCE
1. Open an App with a KUrlRequester widget, like Kdenlive on Windows
2. Click on the button of the KUrlRequester widget to open the file dialog (in Kdenlive Settings > environement)

OBSERVED RESULT
The file dialog always opens in C:\Windows\System32, whatever url is entered in the KUrlRequester lineedit.

EXPECTED RESULT
The file dialog should open the directory and file that appears in the KUrlRequester lineedit.

SOFTWARE/OS VERSIONS
Windows: Windows 11
KDE Frameworks Version: git master (6.18)
Qt Version: 6.8.3

ADDITIONAL INFORMATION
Patching kurlrequester like this fixes the issue ( I can create an MR if that helps):

diff --git a/src/widgets/kurlrequester.cpp b/src/widgets/kurlrequester.cpp
index 4d054cdc9..7c4833cd5 100644
--- a/src/widgets/kurlrequester.cpp
+++ b/src/widgets/kurlrequester.cpp
@@ -230,7 +230,14 @@ public:
             QUrl u(url());
             // If we won't be able to list it (e.g. http), then don't try :)
             if (KProtocolManager::supportsListing(u)) {
-                dlg->selectUrl(u);
+                // selectUrl does not work properly on Native Dialogs
+                if (u.isLocalFile()) {
+                    QFileInfo info(u.toLocalFile());
+                    dlg->setDirectory(info.absolutePath());
+                    dlg->selectFile(info.fileName());
+                } else {
+                    dlg->selectUrl(u);
+                }
             }
         } else {
             dlg->setDirectoryUrl(m_startDir);
Comment 1 Nicolas Fella 2025-08-04 17:22:47 UTC
Looking at the Qt code QFileDialog::selectUrl does seem to do something on Windows, it ends up in https://invent.kde.org/qt/qt/qtbase/-/blob/dev/src/plugins/platforms/windows/qwindowsdialoghelpers.cpp#L1123 and that's calling https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifiledialog-setfilename

The problem seems to be that that doesn't seem to change the folder the dialog opens in?
Comment 2 Nicolas Fella 2025-08-04 17:26:01 UTC
What if you add only the setDirectory() part, before the selectUrl? Does it work as expected then?