PDA

View Full Version : How to make a file dialog with dirs only, multiselection and network places?



tehman
1st August 2012, 07:52
Hey guys

I need to create a file dialog where one can pick directories only with multiselection and there must be network places.

I cannot do all this stuff using the standard possibilities of QFileDialog. The closest method to my problem is QFileDialog::getExistingDirectory (there are only directories, multiselection and network places -- all I need) but it has a bug -- when one selects several folders the method returns "" (an empty string). I tried to rewrite getExistingDirectory in QFileDialog.cpp but in that case I have to rebuild qt dll (QtCore.dll as far as I remember). Maybe there is an easier solution?

How can I solve my problem? I need some help, please.

sonulohani
1st August 2012, 08:39
Use QDir also, for more information go here,
http://www.developer.nokia.com/Community/Wiki/How_to_use_QDir_and_QFileDialog_in_Qt

tehman
1st August 2012, 09:11
Thank you but I need another thing. I need a native standard file dialog where one can choose several folders (multiselection). So in other words I need a kind of re-implemented QFileDialog::getExistingDirectory that returns QStringList instead of QString. I want to get a list of selected folders. So I need my file dialog to look like QFileDialog::getOpenFileNames but for folders, not for files.

sonulohani
1st August 2012, 09:48
#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();
}



The following is the codes that you could try(The original link is http://www.qtcentre.org/threads/34226-QFileDialog-select-multiple-directories?p=220108#post220108):



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

tehman
1st August 2012, 10:28
Thank you again, I have tried your code. Only one problem -- the dialog is not native and there are no network places. :( I have read another discussion board and one said there that only static methods of QFileDialog offered native windows, windows that I need. So I am afraid there is no opportunity to solve my problem completely using QFileDialog. Maybe I should change Qt's source code and build another QtCore.dll to use reimplemented static methods.