QFileDialog: Selecting directories and files

百般思念 提交于 2020-04-21 06:18:32

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/42217120/qfiledialog-selecting-directories-and-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!