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: strange different behaviour of AnyFile and ExistingFile
Forum Updated to NodeBB v4.3 + New Features

QFileDialog: strange different behaviour of AnyFile and ExistingFile

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.9k Views 1 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.
  • L Offline
    L Offline
    lemzwerg
    wrote on last edited by
    #1

    Consider this dialog (using Qt 4.8):

    @
    QFileDialog dialog(this);
    dialog.setDirectory(QDir::homePath());
    dialog.setFileMode(QFileDialog::ExistingFile);
    @

    If I call such a dialogue, it shows the contents of my home directory (say, '/home/wl').

    However, if I replace 'ExistingFile' with 'AnyFile', the dialog shows the contents of '/home', and the directory 'wl' is selected.

    Why is this so? I would expect exactly the same behaviour instead, and the documentation of QFileDialog doesn't mention a difference.

    Interestingly, the static functions 'getOpenFileName' and 'getSaveFileName' do behave the same w.r.t. 'homePath'.

    A bug?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      broadpeak
      wrote on last edited by
      #2

      According to Qt documentation, the two functions aren't the same, read carefully the subtle differentations:

      http://developer.qt.nokia.com/doc/qt-4.8/QFileDialog.html

      " QFileDialog dialog(this);
      dialog.setFileMode(QFileDialog::AnyFile);

      In the above example, the mode of the file dialog is set to AnyFile, meaning that the user can select any file, or even specify a file that doesn't exist . This mode is useful for creating a "Save As" file dialog. Use ExistingFile if the user must select an existing file , or Directory if only a directory may be selected . See the QFileDialog::FileMode enum for the complete list of modes."

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lemzwerg
        wrote on last edited by
        #3

        I'm fully aware of the difference, thanks. However, the Qt documentation doesn't explain why the file dialog starts one level higher (namely in '/home') for AnyFile compared to ExistingFile (which starts in '/home/wl') – with the same start directory argument. This is completely illogical.

        BTW, I'm experiencing this on a GNU/Linux box.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Kol111
          wrote on last edited by
          #4

          I too have the same issue. Qt 4.8.4 Linux Fedora16

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lokifacio
            wrote on last edited by
            #5

            Setting the directory is not enough for QFileDialog with AnyFiles flag. It could mean you want to create a new directory or save your data in existing one. If you want to get inside the given directory you must set a file name to give QFIleDialog a hint about what you really want. Thus, the following code will show the contents of the the directory you've specified (i.e. /home/wl)

            @
            dialog.setDirectory("/home/wl");
            dialog.selectFile("newFile");
            @

            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