简体   繁体   中英

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.

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

I tried this, but the result in my case is a bit strange: I can select a combination of folders and files, as long as the first selected item is a folder.

So when I select a folder, then a file and again a folder, I can proceed clicking the button and retrieving the results: see screenshot in link below.

First folder selected, then file: OK

However, when the first item is a file (followed by a folder, or just a file), the button to proceed is not available... So just selecting one or multiple files is not available to me in this implementation it seems, as can be seen in the other screenshot:

First file selected, then folder: not able to proceed

Is there any way using the same code using QFileDialog that allows you to

  1. select one or more files without selecting folders
  2. selecting one or more folders without selecting files
  3. selecting a combination of files and folders regardless of selection order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM