QFileDialog
-
-
http://www.qtcentre.org/threads/4810-QFileDialog-how-to-choice-File-or-Directory-in-the-same-Dialog
In short, the only solution - write your own dialog. In discussing the details are correct: "It would be hard to create a combined directory and file dialog. Much because it would be impossible to tell if you pick a directory or just want so enter it to look for other directories or files."
QFileDialog has only one button that which takes one of two states: Open (dir) | Select. But you need two buttons in the same time.
-
Oh, I see. Well, the first that comes to mind is to create a widget which contains a button (or radio button group) and a file dialog. A click on a button changes the fileMode() from Directory to ExistingFile and vice versa. I'm not sure this will look great in terms of UI, but I don't see any other easy solution.
-
I just run into a similar problem where the user needs to choose a directory or a file from a File Dialog
Here's my hack to solve it.subclass your class from QFileDialog and set the FileMode to AnyFile
@QFileSystemModel *model = new QFileSystemModel; model->setRootPath(QDir::currentPath()); QStringList filenames; QList<QUrl> urls; urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation)) << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)); mfiledialog->setSidebarUrls(urls); mfiledialog->setFileMode(QFileDialog::AnyFile); mfiledialog->setViewMode(QFileDialog::Detail);
Then have a slot to change FileMode on currentchanged signal as follows
void MainWindow::on_mfiledialog_currentchanged(QString filedir)
{
QFileInfo finfo = QFileInfo(filedir);
if(finfo.isDir())
mfiledialog->setFileMode(QFileDialog::Directory);
else
mfiledialog->setFileMode(QFileDialog::AnyFile);
}
@Edit: please put @ tags around code sections; Andre