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. To select multiple files and dirs using the same QFileDialog.
QtWS25 Last Chance

To select multiple files and dirs using the same QFileDialog.

Scheduled Pinned Locked Moved General and Desktop
6 Posts 5 Posters 21.3k Views
  • 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.
  • N Offline
    N Offline
    nehaR
    wrote on last edited by
    #1

    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.

    Regards,
    Neha

    1 Reply Last reply
    0
    • I Offline
      I Offline
      ijabZhan
      wrote on last edited by
      #2

      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 http://www.qtcentre.org/threads/34226-QFileDialog-select-multiple-directories?p=220108#post220108):

      @QFileDialog* _f_dlg = new QFileDialog(this);
      _f_dlg->setFileMode(QFileDialog::Directory);
      _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) {
      l->setSelectionMode(QAbstractItemView::MultiSelection);
      }
      QTreeView t = _f_dlg->findChild<QTreeView>();
      if (t) {
      t->setSelectionMode(QAbstractItemView::MultiSelection);
      }

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          francomartins
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Frodox
            wrote on last edited by
            #5

            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:

            sfdfiledialog.h
            @
            #ifndef SFDFILEDIALOG_HP
            #define SFDFILEDIALOG_HP

            #include <QFileDialog>

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

            public slots:
            void accept ();
            };

            #endif // SFDFILEDIALOG_HP
            @

            sfdfiledialog.cpp
            @
            #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)
            l->setSelectionMode(QAbstractItemView::ExtendedSelection);

            QTreeView *t = this->findChild<QTreeView*>();
            if (t)
                t->setSelectionMode(QAbstractItemView::ExtendedSelection);
            

            }

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

            SFDFileDialog::~SFDFileDialog()
            {
            }

            void SFDFileDialog::accept()
            {
            QStringList files = selectedFiles();
            if (files.isEmpty())
            return;
            emit filesSelected(files);
            QDialog::accept();
            }
            @

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

            if ( !file_dlg->exec() ) {
            qDebug() << "You select nothing.";
            return;
            }
            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:
            http://www.qtcentre.org/threads/4810-QFileDialog-how-to-choice-File-or-Directory-in-the-same-Dialog

            I hope it will help someone.

            With Best Regards,
            Frodox (:

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Frodox
              wrote on last edited by
              #6

              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.

              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