c++qtqfiledialog

QFileDialog: Selecting directories and files


I'm using the code below to build a qstringlist of filenames:

QStringList filenames = QFileDialog::getOpenFileNames(this,"",QDir::currentPath() );

How can I change this so I can select directories as well?

I looked at:

      dialog.setFileMode(QFileDialog::AnyFile);

but I don't get how to use it with my code.


Solution

  • This code snippet linked in the comment above solves my issue.

    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();