Solved Why is custom tree item data undefined when tree node collapses?
-
I'm using QAbstractItemModel.
The reference sample.
https://github.com/hvoigt/qt-qml-treeview/blob/master/TreeDelegate.qml -
@mirro said in Why is custom tree item data undefined when tree node collapses?:
@sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?
I don't know your code structure. My assumption was that you are using QAbstractItemModel and your radio button state is controlled by
CheckStateRole
.If, on the other hand, you are using some different mechanism, you need to adapt to that. But definitely, regardless of the mechanism - changing a state (any kind of state - item name, check state, color etc.) is a job of the model (or underlying data), not the view.
-
When delegate stops being shown (moves off screen, is clipped or collapsed) QML engine usually destroys it. And on model side, collapsed indexes are invalid so
data()
will return emptyQVariant
.Do this to get rid of the warning:
text: styleData === undefined? "" : styleData.value.name
(and please, please: paste text not pictures where you want to show code! It's much easier for me to then copy-paste your code and show tweaks or corrections)
-
@sierdzio Thank you for your question and answer.
What method can I use to traverse the children under the selected node in QML?
Do I need to add methods to the QAbstractItemModel in C++ ?MouseArea{ OnPressed:{ //... if(styleData.depth === 1) { var curIndex = treeView.indexAt(mouse.x,mouse.y) } } }
-
@mirro said in Why is custom tree item data undefined when tree node collapses?:
@sierdzio Thank you for your question and answer.
What method can I use to traverse the children under the selected node in QML?
Don't do it unless you have to - it's too error prone with delegates coming in and out of existence all the time.
Do I need to add methods to the QAbstractItemModel in C++ ?
MouseArea{ OnPressed:{ //... if(styleData.depth === 1) { var curIndex = treeView.indexAt(mouse.x,mouse.y) } } }
Move your
MouseArea
into the delegate - this way you don't have to find which index and item is affected, becausepressed
signal will be emitted and handled directly in the item which was clicked. -
@sierdzio Hi~So how do I make it so that when I check the main node, all the children are checked together?
-
You need to do it in the model, for sure. This stuff belongs to
setData()
method in the model. -
@sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?
-
@mirro said in Why is custom tree item data undefined when tree node collapses?:
@sierdzio Oh, Do you mean that the QML calls the C++ QAbstractItemModel methods for processing?
I don't know your code structure. My assumption was that you are using QAbstractItemModel and your radio button state is controlled by
CheckStateRole
.If, on the other hand, you are using some different mechanism, you need to adapt to that. But definitely, regardless of the mechanism - changing a state (any kind of state - item name, check state, color etc.) is a job of the model (or underlying data), not the view.