To select multiple files and dirs using the same QFileDialog.

  • Hi All,

    I am working on Qt4.6 on mac os. What i want is, whenever i will open QFileDialog I will able to select both files and folders in the same filedialog at a time and also multiple selection of files and folders.

    I tried to subclass QFileDialog for this but it not opens nativeDialog.

    Is there any other way to open files and folders in the same QFileDialog.


  • I encountered the same questions. And I found a solution after googling in QT forums. The following is the codes that you could try(The original link is

    @QFileDialog* _f_dlg = new QFileDialog(this);
    _f_dlg->setOption(QFileDialog::DontUseNativeDialog, true);

    // Try to select multiple files and directories at the same time in QFileDialog
    QListView l = _f_dlg->findChild<QListView>("listView");
    if (l) {
    QTreeView t = _f_dlg->findChild<QTreeView>();
    if (t) {

    int nMode = _f_dlg->exec();
    QStringList _fnames = _f_dlg->selectedFiles();@

  • For such a selection, I find that using a custom dialog works better. I like having checkboxes in the tree for that, and I wrote a "proxy model":/wiki/QSortFilterProxyModel_subclass_to_add_a_checkbox to support doing that. However, that does mean crafting your own dialog box for the selection. The default platform one does not support it anyway. In my case, I opted to just include the whole tree view in my wizard itself.

  • try is option :
    QStringList ls = QFileDialog::getOpenFileNames();
    return a QStringList of the selecteds Files .

  • I have the same question.
    I found this thread hardly, and ijabZhan's solution looks like what I need.. but it did not work correctly :(
    I cannot select just one(or more) file, or 'file + folder' (in this direction).
    I must select 'folder + file'. It didn't suit me, and the problem was in
    void QFileDialog::accept () [virtual protected]

    method (you could look in Qt's src ;-) ).

    Luckily, I found a solution but in reality it consist of two solutions: above and founded.

    Let's subclass QFileDialog:


    #include <QFileDialog>

    // SelectFileDirDialog
    class SFDFileDialog : public QFileDialog
    SFDFileDialog(QWidget * parent, Qt::WindowFlags flags );
    SFDFileDialog(QWidget * parent = 0, const QString & caption = QString(),
    const QString & directory = QString(),
    const QString & filter = QString() );

    public slots:
    void accept ();

    #endif // SFDFILEDIALOG_HP

    #include "sfdfiledialog.h"
    #include <QListView>
    #include <QTreeView>

    SFDFileDialog::SFDFileDialog(QWidget *parent, const QString &caption, const QString &directory, const QString &filter)
    : QFileDialog(parent, caption, directory, filter)
    // Try to select multiple files and directories at the same time in QFileDialog
    QListView l = this->findChild<QListView>("listView");
    if (l)

    QTreeView *t = this->findChild<QTreeView*>();
    if (t)


    SFDFileDialog::SFDFileDialog(QWidget *parent, Qt::WindowFlags flags)
    : QFileDialog(parent,flags)


    void SFDFileDialog::accept()
    QStringList files = selectedFiles();
    if (files.isEmpty())
    emit filesSelected(files);

    And now you can use it as you want, for example:
    SFDFileDialog *file_dlg = new SFDFileDialog(this, "Select files:");

    if ( !file_dlg->exec() ) {
    qDebug() << "You select nothing.";
    QStringList _fnames = file_dlg->selectedFiles();

    // debug
    QStringList::const_iterator it;
    for (it = _fnames.begin(); it != _fnames.end(); it++)
    qDebug() << *it;
    qDebug() << "-----------";

    delete file_dlg;

    As you noticed, I implanted ijabZhan ’s solution in Constructor of SFDFileDialog, because IMHO it's logically must be there and we have a great chance :) Also you could move it from Constructor, and call later (if you need multi-selection) as in ijabZhan’s post.

    Solution was founded here:

    I hope it will help someone.

    With Best Regards,
    Frodox (:

  • P.s. It work correctly for Qt 4.7.4 and I think for lesser versions too.
    In 4.8.4 dialog close after clicking on 1 file, so it need some other hints.

Log in to reply