Linux – How to use the QT file picker for both GTK and QT apps

arch linuxgtkkdeqt

The file picker for GTK+ apps (eg. Firefox) looks like this:

enter image description here

For qt apps (eg. nomacs) it looks like this:

enter image description here

Is there a way to use the QT file picker for both GTK and QT apps?

I am running arch 4.20 with KDE installed.

Best Answer

Generally, no. GTK and Qt have different file browser dialog windows because the file browser dialog window is an integral part of the toolkit, it's not some external module that you can simply swap with another.


That said, it's kind of possible, but only for GTK 3.20 and later. It will not affect GTK 2 programs. (Actually it won't affect all GTK 3 programs either, only some. Primarily it won't work with apps which add their own custom elements to the dialog, and then most others I don't know why. Fortunately, it works with Firefox, which is probably going to be 95% of your use case.)

To do this, you can force the GTK toolkit to use 'portals' – a Flatpak integration feature, which normally allows sandboxed programs to open various pickers on the host system (outside the sandbox) and receive the result. This necessarily means that the app/toolkit has to somehow call an external picker instead of using its built-in one, and said external picker can be swappable.

In this situation, you want to force-enable this feature without involving Flatpak, and there is a hidden option for that (although meant for developers only – and, as I've already mentioned, works for some apps only).

Additional warning: This is a very broad option and might cause many other operations to go through the 'portal' – such as desktop settings, proxy configuration, and so on. Your mileage may vary. Warranty void if seal broken.

  1. You'll need to install two components:

    • The main portal service (broker), called xdg-desktop-portal.

    • The KDE portal implementation (user interface), called xdg-desktop-portal-kde.

  2. As you're using KDE, it is enough to install these packages and they will be automatically started when needed. Skip the rest of this part and jump straight to step 3.

    Meanwhile, those trying to do this within GNOME will need to start everything manually and add an environment variable forcing both components to use KDE behaviors. First start the KDE-specific implementation, telling it to disable Qt's usual "masquerade as GTK" thing:

    XDG_CURRENT_DESKTOP="KDE" /usr/lib/xdg-desktop-portal-kde &
    

    Then start the portal broker, using the same environment variable to make it choose the earlier-started KDE portal implementation and not the GNOME one:

    XDG_CURRENT_DESKTOP="KDE" /usr/lib/xdg-desktop-portal &
    

    Alternatively, to make everything automagically start via D-Bus, you might want to pass these environment variables to dbus-daemon and systemd instead:

    dbus-update-activation-environment --systemd XDG_CURRENT_DESKTOP="KDE"
    systemctl --user stop xdg-desktop-portal{,-gtk}
    pkill -f xdg-desktop-portal
    
  3. Finally, start your GTK3-based app, telling to use portals even outside Flatpak environment:

    GTK_USE_PORTAL="1" zenity --file-selection
    GTK_USE_PORTAL="1" firefox
    

From my tests, this works with Zenity, Firefox, GEdit, but does not work with Mousepad or Evince. (I haven't tested Chromium but it already has its own swappable file pickers anyway.)

Related Question