Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QDirIterator over subdirectories in TreeWidget?



  • Howdy fellow QT'ers, I'm adding this next feature to my personal learning plan and was wondering if you pros can give some tips. I got the tree structure of the following directory that has many subdirectories loaded with drivers.
    0599c2f7-5626-40c6-919c-de3860ffe34b-image.png

    Unfortunately, this is what I getting now: (hoping to imitate the tree structure)
    8bfbfa82-eafa-4c65-ab5d-0e2496989bee-image.png

    mainWindow.cpp
    
    void MainWindow::PopulateTree()
    {
        QString testDirectoryPath = "C://Temp//MSI Drivers";
        QDirIterator dirIterator(testDirectoryPath, QDir::AllDirs, QDirIterator::Subdirectories);
        itemsParent = new QTreeWidgetItem(ui->treeWidget);
        itemsParent->setText(0, testDirectoryPath);
        while (dirIterator.hasNext())
        {
           QString dirItem(dirIterator.next());
           QFileInfo dirItemInfo(dirItem);
    
           if (dirItemInfo.isDir())
           {
               itemsParent = new QTreeWidgetItem(ui->treeWidget);
               itemsParent->setText(0, dirItemInfo.fileName());
           }
           else
           {
               itemsChild = new QTreeWidgetItem(ui->treeWidget);
               itemsChild->setText(0, dirItemInfo.fileName());
               itemsParent->addChild(itemsChild);
           }
        }
    }
    


  • @BoGut

    void MainWindow::initTreeView()
    {
        dirModel = new QFileSystemModel(this);
        dirModel->setRootPath("");
        dirModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
        ui->treeView->setModel(dirModel);
    }
    


  • Thanks for the reply, so I added the following:

    void MainWindow::PopulateTree()
    {
        QString testDirectoryPath = "C://Temp//MSI Drivers";
        dirModel = new QFileSystemModel(this);
        dirModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
        dirModel->setRootPath(testDirectoryPath);
        ui->treeView->setModel(dirModel);
    }
    

    and had to change to a QTreeView as QTreeWidget has a private setModel. I'm getting all of the C drive displaying even though I'm setting the setRootPath to C://Temp//MSI Drivers



  • @BoGut said in QDirIterator over subdirectories in TreeWidget?:

    "C://Temp//MSI Drivers"

    At a guess: the double forward slashes make this an incorrect path, maybe it just respects the leading C:.



  • Tried with single slashes and it still displays the full HD. So weird

    QString testDirectoryPath = "C:/Temp/MSI Drivers";
    


  • @BoGut
    As per https://doc.qt.io/qt-5/qfilesystemmodel.html#example-usage, I think you also have to set QTreeView::setRootIndex()

    and the contents of a particular directory can be displayed by setting the tree view's root index:

    tree->setRootIndex(model->index(QDir::currentPath()));
    

    The view's root index can be used to control how much of a hierarchical model is displayed. QFileSystemModel provides a convenience function that returns a suitable model index for a path to a directory within the model.



  • Latest code changes and I only get this displaying now. There's no files being displayed nor any subdirectories past the path I supplied.

    void MainWindow::PopulateTree()
    {
        QString testDirectoryPath = "C:/Temp/MSI Drivers";
        dirModel = new QFileSystemModel(this);
        dirModel->setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
        ui->treeView->setRootIndex(dirModel->index(testDirectoryPath));
        ui->treeView->setModel(dirModel);
    }
    

    5e7ba2fc-71bd-420b-8f9b-577b86bfe608-image.png


  • Lifetime Qt Champion

    Hi,

    Set the model before setting the root index, otherwise it will have no meaning.



  • @SGaist Hi, doing that displays nothing in the treeView.


  • Lifetime Qt Champion

    Then check the validity of the index. Just for the sake of testing, use a folder without a space in it.



  • Thanks for the help everyone, I got to step away from my coding learn journey for now. New work came in and I will be outside quite a bit, little time to sit in front of my PC. I'll continue to implement the ideas you guys mentioned and once it works, I'll post the final solution. Thanks and all the best!


Log in to reply