QFileDialog for selecting a directory, and cross-platform



  • I want a QFileDialog for selecting an existing directory. I develop under Linux, but code must also run under Windows, and I don't have (refuse to have :) ) the app/Qt running under Windows.

    I tried the simple QFileDialog::getExistingDirectory() (http://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory). I found it did not fully respect some of the options I tried to pass in. I also got severely confused by the documentation there, especially to do with native dialogs and Windows behaviour. I also found several undesirable features in Linux native chooser. See my comments in code below.

    So to cut a long story short, I think I'm best with an explicit instance implementation rather than the helper status function.

    I have gone for (Python):

    fileDialog = QtWidgets.QFileDialog(self, "Choose directory")
    # directory only
    fileDialog.setFileMode(QtWidgets.QFileDialog.DirectoryOnly)
    # just list mode is quite sufficient for choosing a diectory
    fileDialog.setViewMode(QtWidgets.QFileDialog.List)
    # only want to to show directories
    fileDialog.setOption(QtWidgets.QFileDialog.ShowDirsOnly)
    # native dialog, at least under Ubuntu GNOME is a bit naff for choosing a directory
    # (shows files but greyed out), so going for Qt's own cross-plaform chooser
    fileDialog.setOption(QtWidgets.QFileDialog.DontUseNativeDialog)
    # get rid of (or at least grey out) file-types selector
    fileDialog.setOption(QtWidgets.QFileDialog.HideNameFilterDetails)
    # DontResolveSymlinks seemingly recommended by http://doc.qt.io/qt-5/qfiledialog.html#getExistingDirectory
    # but I found it didn't make any difference (symlinks resolved anyway)
    # fileDialog.setOption(QtWidgets.QFileDialog.DontResolveSymlinks)
    
    if not fileDialog.exec_():
        return
    dir = fileDialog.selectedFiles()[0]
    

    This gives me an Ubuntu dialog:
    alt text

    1. (Most important) Would some kind soul confirm it will look/behave more or less like this under Windows too, please? (Also, if anything wrong/dubious/missing in my options/code let me know.)

    2. (Just an observation, seemingly not vital) Qt docs seem to recommend/suggest option QFileDialog.DontResolveSymlinks, but I experimented with symlinks to directories and couldn't find any difference in behaviour/selection with it on or off. Anyone know specifically what this might/is supposed to do, am I fine without it?

    TIA.


  • Moderators

    @JNBarchan said in QFileDialog for selecting a directory, and cross-platform:

    (Most important) Would some kind soul confirm it will look/behave more or less like this under Windows too, please? (Also, if anything wrong/dubious/missing in my options/code let me know.)

    Yes, the non-native dialog is the same on all platforms (except for the icons - these are more OS-dependent). Although last time I actually tried it out on Windows was ~5 years ago so I might be wrong here.



  • Thanks.

    Before marking this thread Solved, anyone on:

    2.(Just an observation, seemingly not vital) Qt docs seem to recommend/suggest option QFileDialog.DontResolveSymlinks, but I experimented with symlinks to directories and couldn't find any difference in behaviour/selection with it on or off. Anyone know specifically what this might/is supposed to do, am I fine without it?

    ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.