Unsolved 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.
Unfortunately, this is what I getting now: (hoping to imitate the tree structure)
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); } } }
-
-
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 yourPopulateTree()
function again passing in the directory as parent of new nodes and using that as parent in thenew QTreeWidgetItem()
calls. -
Are you aware that you do not have to do any of this via
QDirIterator
and recursion yourself? Qt hasQFileSystemModel
, 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); }
-
-
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 setQTreeView::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); }
-
Hi,
Set the model before setting the root index, otherwise it will have no meaning.
-
@SGaist Hi, doing that displays nothing in the treeView.
-
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!