1
votes

i have a QFileDialog object where i need the user to select a directory

QFileDialog dlg(this, tr("Select directory"));
dlg.setDirectory(currentDir);
dlg.setFileMode(QFileDialog::Directory);
dlg.setOptions(QFileDialog::ShowDirsOnly | QFileDialog::ReadOnly);

my folder structure is for example

<root>
  <dir1>
  <dir2>

when the currentDir is <root>/dir1 i want the dialog to point on the <root> dir and select dir1 in the dialog (not only in text form but also like when you have clicked it)

is that possible, and how?

/edit: i tried

dlg.setDirectory("<root>");
dlg.selectFile("dir1");

that resulted in the dialog to set the correct directory and fill the filename line edit but does not select it in the list of files (but thats quite needed when having a lot of similar dir names).

cheers

1
I tried several things with QFileDialog but it did not work. Qt 5.6.0 here, though. Using QUrl did not change the behaviour. Neither the selection is adjusted nor the text box filled. This might be a bug. Also adjusting some flags without success. As the documentation says selectFile should be the way. Maybe there is an issue here.maxik

1 Answers

1
votes

selectFile() should do the trick:

QFileDialog dlg(this, tr("Select directory"));

if(currentDir == "<root>/dir1") {
    dlg.selectFile(currentDir);
}
else {
    dlg.setDirectory(currentDir);
}

dlg.setFileMode(QFileDialog::Directory);
dlg.setOptions(QFileDialog::ShowDirsOnly | QFileDialog::ReadOnly);