Bug 505046

Summary: Accepting folder chooser dialog without otherwise interacting with it always returns $HOME
Product: [Plasma] plasma-integration Reporter: Tobias Leupold <tl>
Component: generalAssignee: Plasma Bugs List <plasma-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: kdelibs-bugs-null, nate, tl
Priority: NOR    
Version First Reported In: 6.4.80   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In: 6.3.6
Sentry Crash Report:
Attachments: CMakeLists.txt
main.cpp
MainWindow.cpp
MainWindow.h

Description Tobias Leupold 2025-05-31 09:28:54 UTC
Hi all,

this bug surfaced after porting KPhotoAlbum to Qt 6. We use a persistent "Copy/Link to" object that helps copying or linking images to the same target directory, keeping the last one as a preset. However, it happened that not all images e.g. copied to some directory actually ended in there.

A closer inspection showed that the "native" (i.e. KDE) file dialog does not return the preset directory if one simply presses enter, without clicking on some directory. Instead, the home directory is returned in this case, although the preset directory is shown and selected in the dialog.

Using Qt's native file dialogs works as expected.

I'll attach a minimal example …
Comment 1 Tobias Leupold 2025-05-31 09:29:29 UTC
Created attachment 181918 [details]
CMakeLists.txt
Comment 2 Tobias Leupold 2025-05-31 09:29:41 UTC
Created attachment 181919 [details]
main.cpp
Comment 3 Tobias Leupold 2025-05-31 09:29:53 UTC
Created attachment 181920 [details]
MainWindow.cpp
Comment 4 Tobias Leupold 2025-05-31 09:30:04 UTC
Created attachment 181921 [details]
MainWindow.h
Comment 5 Tobias Leupold 2025-05-31 09:35:56 UTC
Steps to see the issue: Compile and ruin the demo obviously ;-) Then:

- Click "Get dir (native)"
- Choose any directory ("/home/tobias/tmp/9" in my case)
- Click "Get dir (native)" again
- Press Enter

Check the console output:
    Native file chooser triggered
        m_lastNative is ""
         "/home/tobias/tmp/9"
        m_lastNative is now "/home/tobias/tmp/9"
    Native file chooser triggered
        m_lastNative is "/home/tobias/tmp/9"
         "/home/tobias"
        m_lastNative is now "/home/tobias"

First time, "/home/tobias/tmp/9" is returned, as expected.
Second time, "/home/tobias/tmp/9" is pre-selected – but the returned directory is "/home/tobias".

Interestingly, this does not happen if you click "OK" using the mouse instead of pressing Enter. In this case, the correct directory is returned.

The second button is to compare with Qt's file dialogs. There, it doesn't matter if you click "Select" or press Enter. Which is the expected behavior.
Comment 6 Nate Graham 2025-06-03 22:31:05 UTC
Can reproduce.
Comment 7 Nate Graham 2025-06-06 21:01:53 UTC
The problem here is that pressing the Enter key quickly selects $HOME and then accept the dialog. This doesn't happen if you first manually focus the tree view before pressing Enter, or if you press the OK button instead.

Smells like focus handling shenanigans.
Comment 8 Nate Graham 2025-06-06 21:28:21 UTC
Oh! It's because the places panel invisibly has focus by default. So pressing Enter accepts the combobox with the first item in the places panel — which is generally $HOME.
Comment 9 Bug Janitor Service 2025-06-06 21:34:45 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/plasma-integration/-/merge_requests/176
Comment 10 Tobias Leupold 2025-06-07 06:35:24 UTC
Hey, thanks a lot for tracking this down and for the immediate fix :-) I wanted to check if I could find it myself, but I wasn't sure where to to find the respective code … however, you were faster ;-)
Comment 11 Nate Graham 2025-06-09 14:37:15 UTC
Git commit 5fe925f940be709ac2abde5a8b6f7c77296a6bd4 by Nate Graham.
Committed on 06/06/2025 at 21:30.
Pushed by ngraham into branch 'master'.

kdirselectdialog: correct initial focus

It begins with focus on the top-left-most widget, which normally is
good. However here that means the Places panel, which is not what you
want; you can't start navigating the tree view immediately, and if you
pres the Enter key, the top-most Places item gets returned instead of
the item visibly selected in the tree view.

Instead, start with focus on the main view, which corrects both issues.
FIXED-IN: 6.3.6

M  +2    -0    qt5/src/platformtheme/kdirselectdialog.cpp
M  +2    -0    qt6/src/platformtheme/kdirselectdialog.cpp

https://invent.kde.org/plasma/plasma-integration/-/commit/5fe925f940be709ac2abde5a8b6f7c77296a6bd4
Comment 12 Nate Graham 2025-06-09 14:40:36 UTC
Git commit 46625af3be99ca43591745054d6d016983cd6800 by Nate Graham.
Committed on 09/06/2025 at 14:38.
Pushed by ngraham into branch 'Plasma/6.4'.

kdirselectdialog: correct initial focus

It begins with focus on the top-left-most widget, which normally is
good. However here that means the Places panel, which is not what you
want; you can't start navigating the tree view immediately, and if you
pres the Enter key, the top-most Places item gets returned instead of
the item visibly selected in the tree view.

Instead, start with focus on the main view, which corrects both issues.
FIXED-IN: 6.3.6


(cherry picked from commit 5fe925f940be709ac2abde5a8b6f7c77296a6bd4)

Co-authored-by: Nate Graham <nate@kde.org>

M  +2    -0    qt5/src/platformtheme/kdirselectdialog.cpp
M  +2    -0    qt6/src/platformtheme/kdirselectdialog.cpp

https://invent.kde.org/plasma/plasma-integration/-/commit/46625af3be99ca43591745054d6d016983cd6800
Comment 13 Nate Graham 2025-06-09 14:43:23 UTC
Git commit 29b7cbc42101bac4bcbc96b1c3201ad442a11650 by Nate Graham.
Committed on 09/06/2025 at 14:39.
Pushed by ngraham into branch 'Plasma/6.3'.

kdirselectdialog: correct initial focus

It begins with focus on the top-left-most widget, which normally is
good. However here that means the Places panel, which is not what you
want; you can't start navigating the tree view immediately, and if you
pres the Enter key, the top-most Places item gets returned instead of
the item visibly selected in the tree view.

Instead, start with focus on the main view, which corrects both issues.
FIXED-IN: 6.3.6


(cherry picked from commit 5fe925f940be709ac2abde5a8b6f7c77296a6bd4)

Co-authored-by: Nate Graham <nate@kde.org>

M  +2    -0    qt5/src/platformtheme/kdirselectdialog.cpp
M  +2    -0    qt6/src/platformtheme/kdirselectdialog.cpp

https://invent.kde.org/plasma/plasma-integration/-/commit/29b7cbc42101bac4bcbc96b1c3201ad442a11650