Qt World Summit: Register Today!

Using QFileDialog to select a project directory

  • I'm working on an application that keeps project information in a directory, instead of a simple file. This directory name has the suffix ".iab". Is it possible to convince a QFileDialog to constrain users to selecting directories with .iab suffix?

    An example: If a user is looking for the project "foo", I would like to set the name filter to *.iab and have the user browse around for the "file" foo.iab. This file is actually implemented as a directory, but I want the file browser to present *.iab files as the goal.

    I've made a go at it, but it's not working consistently. On Mac OS X I setFilter(QDir::Dirs) and setNameFilter("Icarus Astrobench Project (*.iab)"), and it sorta works. But on Linux, it seems to be impossible to select that directory unless I add setFileMode(QFileDialog::Directory).

  • from the "docs":http://doc.qt.nokia.com/4.7/qfiledialog.html:
    fileName = QFileDialog::getOpenFileName(this,
    tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

  • vinb: are you sure that also works for selecting directories?

    steveicarus: if you want to select a directory, doesn't it make sense to use a mode for QFileDialog that is actually geared to do that? Why is it a problem to use setFileMode(QFileDialog::Directory)?

  • @Andre: ehm.. :) my mistake, didnt read the question properly. Thanks for pointing that out in a subtle way.

  • [quote author="vinb" date="1303817850"]@Andre: ehm.. :) my mistake, didnt read the question properly. Thanks for pointing that out in a subtle way.
    Happens to me too :-)

  • When I use:
    setNameFilter("... (*.iab)")@
    and try it on Mac OS X, the foo.iab selections are grayed out and any directories except foo.iab are selectable; exactly opposite what I want: I only want to select directories named foo.iab. It's closer when I do not setFileMode to Directory. Apparently, the setNameFilter takes precedence there.

    But on Linux, I cannot select any directories, not even foo.iab, unless the QFileDialog::Directory mode is set, but then on Linux the setNameFilter filter is ignored for directories. The experience is not quite right.

    I guess what I really want is for the QFileDialog to pretend that foo.iab directories are bundles, and I need it to work on Mac OS X and Linux. I'm OK with deriving from QFileDialog to implement more complex selection criteria, if that's what it takes.

  • You can also add the option QFileDialog::ShowDirsOnly.

Log in to reply