TreeView getting all children in a TreeItemModel is empty
-
I am new to Qt. Trying to learn. I am using Qt 5, since it comes with my Linux distribution KDE, I wanted to use that version instead of Qt 6.
I am having some trouble with TreeView and TreeItemModel.
I am using the QStandardItemModel and QStandardItem classes.
Though I have made my own subclasses of them.class SubsystemItemModel : public QStandardItemModel class SubsystemItem : public QStandardItem
I did try to follow the Simple Tree Model Example, but found it a bit complex. I found using QAbstractItemModel was a bit to complex for my level of Qt understanding. To much I had to implement on my own, and I did not need multiple columns in my TreeView data.
https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.htmlI am trying to get all the children in the model, all the items.
I have tried getting them by calling both of these two methods on the QStandardItemModel
https://doc.qt.io/qt-5/qobject.html#children
https://doc.qt.io/qt-5/qobject.html#findChildrenHowever both lists are empty.
SubsystemItemModel subsystem_model = new SubsystemItemModel(mw, this); subsystem_model->setHorizontalHeaderLabels(QStringList() << "Subsystems"); QTreeView *treeView = ui->subsystemTreeView; treeView->setModel(subsystem_model); SubsystemItem *item0 = new SubsystemItem("TEST"); subsystem_model->appendRow(item0); SubsystemItem *item01 = new SubsystemItem("TEST.SUBTEST_1"); item0->appendRow(item01); SubsystemItem *item02 = new SubsystemItem("TEST.SUBTEST_2"); item0->appendRow(item02); SubsystemItem *item03 = new SubsystemItem("TEST.SUBTEST_3"); item0->appendRow(item03); SubsystemItem *item04 = new SubsystemItem("TEST.SUBTEST_4"); item0->appendRow(item04); SubsystemItem *item05 = new SubsystemItem("TEST.SUBTEST_5"); item0->appendRow(item05); std::cout << "children" << std::endl; QObjectList children = subsystem_model->children(); foreach (auto obj, children) { std::cout << "child " << obj->objectName().toStdString() << std::endl; } std::cout << "findChildren" << std::endl; QList<QObject*> list = subsystem_model->findChildren<QObject*>(); foreach (auto obj, children) { std::cout << "child " << obj->objectName().toStdString() << std::endl; }
The items are shown in the TreeView when I run my application, but both these lists are empty.
-
Since QStandardItem is not derived from QObject neither children() nor qFindChild() can return them.
You're looking for invisibleRootItem() where you then can traverse through all the child items. -
@Christian-Ehrlicher
I thought so too. I was checking into using the invisibleRootItem(), but cannot find which method to call upon.
https://doc.qt.io/qt-5/qstandarditem.htmlI tried this, but I am not getting all the children.
QStandardItem *rootItem = subsystem_model->invisibleRootItem(); auto rowCount = rootItem->rowCount(); std::cout << "Rows " << std::to_string(rowCount) << std::endl; for (int row = 0; row < rowCount; ++row) { QStandardItem *item = rootItem->child(row); std::cout << item->text().toStdString() << std::endl; }
The rowCount=1. I guess that is right, considering I only have one row added on the model. Then the first row has 5 items.
Do I need to recursive traverse my self to get all items? -
@DJViking said in TreeView getting all children in a TreeItemModel is empty:
Do I need to recursive traverse my self to get all items?
Yes, it's a tree.