PDA

View Full Version : QFileDialog select multiple directories



bepaald
16th September 2010, 12:18
Using a QFileDialog, I can allow the user to select one or more files, or to select one directory. Is there any way to allow selecting multiple directories?

I have found this FAQ (http://developer.qt.nokia.com/faq/answer/when_the_filemode_is_directoryonly_in_my_qfiledial og_how_can_i_select_multi) on the qt-site, but the code there doesn't work (even after fixing the typos).

Any ideas?

thanks,
bepaald

e8johan
16th September 2010, 14:01
It seems that the QFileDialog won't do it out of the box. My solution would be to build a new directory picker dialog for your application, supporting multiple directories. By using a QFileSystemModel, you don't have to worry about traversing the file system.

JohannesMunk
16th September 2010, 20:40
My guess would be that the solution posted in the FAQ can't work with the OS-native filedialogs, which are used by default.
The native dialog won't have a QListWidget etc..

Try opening a qt one.


filedialog.setOption(QFileDialog::DontUseNativeDia log,true);


Joh

JohannesMunk
16th September 2010, 21:57
I just checked; multiselection works:


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFileDialog w;
w.setFileMode(QFileDialog::DirectoryOnly);
w.setOption(QFileDialog::DontUseNativeDialog,true) ;
QListView *l = w.findChild<QListView*>("listView");
if (l) {
l->setSelectionMode(QAbstractItemView::MultiSelection );
}
QTreeView *t = w.findChild<QTreeView*>();
if (t) {
t->setSelectionMode(QAbstractItemView::MultiSelection );
}
return w.exec();
}


It works no matter what I set the DontUseNativeDialog to. I can't seem to get the native dialog going! Strange!

I'm on Win7. Qt4.7. Mingw.

Joh

ijab
15th May 2012, 05:17
The following codes are a good solution to select multiple directories and files in the same QFileDialog. Thanks!


I just checked; multiselection works:


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFileDialog w;
w.setFileMode(QFileDialog::DirectoryOnly);
w.setOption(QFileDialog::DontUseNativeDialog,true) ;
QListView *l = w.findChild<QListView*>("listView");
if (l) {
l->setSelectionMode(QAbstractItemView::MultiSelection );
}
QTreeView *t = w.findChild<QTreeView*>();
if (t) {
t->setSelectionMode(QAbstractItemView::MultiSelection );
}
return w.exec();
}


It works no matter what I set the DontUseNativeDialog to. I can't seem to get the native dialog going! Strange!

I'm on Win7. Qt4.7. Mingw.

Joh