Search code examples
qtubuntuqt5ubuntu-16.04qfiledialog

How do I get the system QFileDialog in Qt5?


I created file dialogs to select files within a Qt application. When migrating to Qt5 I encounter a regression, the file dialog is much poorer when linking against Qt5 rather than Qt4:

Qt4

Qt4 file dialog

Qt5

Qt5 file dialog

For example it is very hard for an user to navigate to the network mounted share directories.

The code is as simple as this:

QString path;
path = QFileDialog::getExistingDirectory(this, tr("Pick a file"));

QFileDialog::DontUseNativeDialog does not change anything.

My OS is Ubuntu 16.04, I have both nautilus (3.14.3) and nemo (2.8.7) installed. nemo is configured to be the default file manager:

xdg-mime default nemo.desktop inode/directory application/x-gnome-saved-search

Every other application seems to use the same dialog as the Qt4 file dialog screenshot.

How do I get the "old" file dialog in Qt5?


If I try with QT_QPA_PLATFORMTHEME= ./my_app then I get this:

enter image description here


Solution

  • The following works for me on Ubuntu 16.04 – it is more of a workaround than an actual solution, though:

    Edit: First make sure that the package libqt5libqgtk2 is installed. It brings the GTK2 bindings for Qt5. If the package is not installed, the file choosers will look like the last screenshot shown in the question above.

    Then, assuming you want to launch your application my_app from a terminal, launch it as follows (mind the space after =):

    $ QT_QPA_PLATFORMTHEME= my_app
    

    This is because, according to a bug report on launchpad, there seems to be a problem with the Ubuntu package appmenu-qt5: the package forces Qt5 dialogs to become non-native as a side effect of explicitly setting QT_QPA_PLATFORMTHEME=appmenu-qt5 via /etc/profile.d/appmenu-qt5.sh. Unsetting the platform theme via QT_QPA_PLATFORMTHEME= before launching your application changes this behavior locally.

    Edit: When using the described approach, however, the global menu might not work in my_app.