Create open multiselect dialog to only directories

  • I found this code:
    QFileDialog* _f_dlg = new QFileDialog(this);
    _f_dlg->setOption(QFileDialog::DontUseNativeDialog, true);

    QListView *l = _f_dlg->findChild<QListView*>("listView");
    if (l)
    QTreeView *t = _f_dlg->findChild<QTreeView*>();
    if (t) {
    int nMode = _f_dlg->exec&#40;&#41;;
    QStringList _fnames = _f_dlg->selectedFiles();


    But, this code return the path of current directory. For example: in "C:", I go inside of "images" and inside I choose 3 directories (folder1, folder2, folder3). But the result is a vector of strings of 4 elements:

    • C:\images

    • C:\images\folder1

    • C:\images\folder2

    • C:\images\folder3

    I don't understand because. I want to remove that element (the path)

  • Moderators

    Ouch, modifying the dialog like that. That's ugly and will break when they decide to change eg. "listview" to "someList".
    I would strongly suggest to implement your own dialog for that using eg. "QFileSystemModel":

    If not then as you're "doing it wrong" already you might just as well modify the returned list the way you wanted:
    std::for_each(_fnames.begin(), _fnames.end(), [](QString& s){ s = QDir(s).dirName(); });

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.