QFileDialog: strange different behaviour of AnyFile and ExistingFile



  • 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?



  • 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."



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



  • I too have the same issue. Qt 4.8.4 Linux Fedora16



  • 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");
    @


Log in to reply
 

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