Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog for selecting a directory, and cross-platform

QFileDialog for selecting a directory, and cross-platform

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 2.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #1

    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.

    sierdzioS 1 Reply Last reply
    0
    • JonBJ JonB

      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.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @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.

      (Z(:^

      1 Reply Last reply
      1
      • JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        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?

        ?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved