QFileDialog does not currently support this. I think the main problem for you here is that FileMode is not Q_FLAGS , and the values are not 2, and therefore you cannot write this to solve this problem.
setFileMode(QFileDialog::Directory|QFileDialog::ExistingFiles)
To solve this problem, you need to bother a lot, for example:
My attempt below demonstrates the first, but I really have not gone as far as the second decides, because this, apparently, is associated with even more messing with the model of choice.
main.cpp
#include <QFileDialog> #include <QApplication> #include <QWidget> #include <QTreeWidget> #include <QPushButton> #include <QStringList> #include <QModelIndex> #include <QDir> #include <QDebug> class FileDialog : public QFileDialog { Q_OBJECT public: explicit FileDialog(QWidget *parent = Q_NULLPTR) : QFileDialog(parent) { setOption(QFileDialog::DontUseNativeDialog); setFileMode(QFileDialog::Directory); // setFileMode(QFileDialog::ExistingFiles); for (auto *pushButton : findChildren<QPushButton*>()) { qDebug() << pushButton->text(); if (pushButton->text() == "&Open" || pushButton->text() == "&Choose") { openButton = pushButton; break; } } disconnect(openButton, SIGNAL(clicked(bool))); connect(openButton, &QPushButton::clicked, this, &FileDialog::openClicked); treeView = findChild<QTreeView*>(); } QStringList selected() const { return selectedFilePaths; } public slots: void openClicked() { selectedFilePaths.clear(); qDebug() << treeView->selectionModel()->selection(); for (const auto& modelIndex : treeView->selectionModel()->selectedIndexes()) { qDebug() << modelIndex.column(); if (modelIndex.column() == 0) selectedFilePaths.append(directory().absolutePath() + modelIndex.data().toString()); } emit filesSelected(selectedFilePaths); hide(); qDebug() << selectedFilePaths; } private: QTreeView *treeView; QPushButton *openButton; QStringList selectedFilePaths; }; #include "main.moc" int main(int argc, char **argv) { QApplication application(argc, argv); FileDialog fileDialog; fileDialog.show(); return application.exec(); }
main.pro
TEMPLATE = app TARGET = main QT += widgets CONFIG += c++11 SOURCES += main.cpp
Assembly and launch
qmake && make && ./main
source share