Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDirIterator over subdirectories in TreeWidget?
Forum Updated to NodeBB v4.3 + New Features

QDirIterator over subdirectories in TreeWidget?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BoGut
    wrote on last edited by
    #1

    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);
           }
        }
    }
    
    JonBJ 1 Reply Last reply
    0
    • B BoGut

      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);
             }
          }
      }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @BoGut

      • You are not doing any recursion. To achieve the nested tree structure, in your isDir() case after you have set the node text you will need to recurse calling something like your PopulateTree() function again passing in the directory as parent of new nodes and using that as parent in the new QTreeWidgetItem() calls.

      • Are you aware that you do not have to do any of this via QDirIterator and recursion yourself? Qt has QFileSystemModel, see e.g. the example at https://www.bogotobogo.com/Qt/Qt5_QTreeView_QFileSystemModel_ModelView_MVC.php, or even just the simple code at the start of https://www.qtcentre.org/threads/53173-Simple-QFileSystemModel-and-TreeView-Example

      void MainWindow::initTreeView()
      {
          dirModel = new QFileSystemModel(this);
          dirModel->setRootPath("");
          dirModel->setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
          ui->treeView->setModel(dirModel);
      }
      
      1 Reply Last reply
      5
      • B Offline
        B Offline
        BoGut
        wrote on last edited by
        #3

        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

        JonBJ 1 Reply Last reply
        0
        • B BoGut

          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

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @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:.

          1 Reply Last reply
          1
          • B Offline
            B Offline
            BoGut
            wrote on last edited by
            #5

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

            QString testDirectoryPath = "C:/Temp/MSI Drivers";
            
            JonBJ 1 Reply Last reply
            0
            • B BoGut

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

              QString testDirectoryPath = "C:/Temp/MSI Drivers";
              
              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @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.

              1 Reply Last reply
              1
              • B Offline
                B Offline
                BoGut
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

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

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  B 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    Hi,

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

                    B Offline
                    B Offline
                    BoGut
                    wrote on last edited by BoGut
                    #9

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

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

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

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      1
                      • B Offline
                        B Offline
                        BoGut
                        wrote on last edited by
                        #11

                        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!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved