iterate through treeview (QAbstractItemModel) of an older topic (see link)
-
Re: https://forum.qt.io/post/647207
The code above in an older topic discribes nearly what I need. But now my question to it: how can I iterate over all items of the tree when it has been created?
e.g. to write data of the tree to a csv file
-
Re: https://forum.qt.io/post/647207
The code above in an older topic discribes nearly what I need. But now my question to it: how can I iterate over all items of the tree when it has been created?
e.g. to write data of the tree to a csv file
@MaMo
You should (probably) write a recursive function to descend the tree depth-first, taking into account that tree models have indexes which require you to pass the correct parent. have a read of https://doc.qt.io/qt-6/qmodelindex.html#details and https://doc.qt.io/qt-6/model-view-programming.html#navigation-and-model-index-creation.You might be able to get a list of all the indexes via https://doc.qt.io/qt-6/qabstractitemmodel.html#match with something like:
QModelIndexList matches = model->match(rootIndex, Qt::DisplayRole, "", -1, Qt::MatchFlags(Qt::MatchStartsWith | Qt::MatchRecursive));[Just seen you are Python, so Python equivalent.] But this pre-generates the entire list, and per the docs we are not sure what order it comes out in. I prefer the self-coded recursive approach.
Be aware that writing to a CSV file will produce a flat output, i.e. you will not know where items are in the tree hierarchy, two adjacent items might be siblings, parent->child, or child->parent. If you don't care about that it's fine, if you do care you will need to output some extra column into the CSV to indicate parent/childhood.
-
Csv was just an example. whether i export it as csv or xml doesn't matter for now. My problem at the moment is to get all the data from the treeview, e.g. after clicking on a button → unfortunately i can't do that.
@MaMo said in iterate through treeview (QAbstractItemModel) of an older topic (see link):
unfortunately i can't do that.
I suspect you mean, or should mean, from the model rather than from the
QTreeView. Presumably all the information you want is in the model;QTreeViewis aQWidget(and complex at that), I don;t think you will want to extract its information.So I suggested above how to go about getting the information from the model. You just need to write the code for what you want.
-
@MaMo said in iterate through treeview (QAbstractItemModel) of an older topic (see link):
unfortunately i can't do that.
I suspect you mean, or should mean, from the model rather than from the
QTreeView. Presumably all the information you want is in the model;QTreeViewis aQWidget(and complex at that), I don;t think you will want to extract its information.So I suggested above how to go about getting the information from the model. You just need to write the code for what you want.
-
@JonB
And how exactly do i get the model for the example above when I click on a button in Python??@MaMo said in iterate through treeview (QAbstractItemModel) of an older topic (see link):
And how exactly do i get the model for the example above when I click on a button in Python??
Well, you're creating the model, right? So, just store it as member variable.
-
@JonB
And how exactly do i get the model for the example above when I click on a button in Python??@MaMo
As @jsulm says, you created the model, right? So you can keep it in a member variable of whatever UI class you are in, perhaps where theQTreeViewis, perhaps it's yourQMainWindowor something. Or, wherever it is, you set it viaQTreeView.setModel()so you can always retrieve it viaQTreeView.model().