1
votes

I have a hierarchical datastructure which I wrapped in a QModel (inherited from QAbstractItemModel) and which I show and edit in a QTreeView.

Let's assume that QTreeView shows the following data:

Item1  
|----Item2  
     |----Item3  
          |-----Item4  
                |----Item5

Now the following shall happen:
1) I edit Item3 and change it's value to Item3_a.
2) The QModel recognizes the change and changes the items' values of parents and children in the wrapped model to:

Item1_a
|----Item2_a
     |----Item3_a
          |-----Item4_a
                |----Item5_a

3) The QTreeView gets informed by the model about the additional changes (Item1,2,4 and 5). Only the displayed values are changed. The hierarchical structure remains the same.

My questions aims on step 3:

How do I notify the QTreeView about the changed data properly?

This is what I tried:

I know that there is modelReset, but then the QTreeView gets collapsed. However it should keep its collpase/expanded state.

According to the docs using models setData method with different parent indices gives undefined behaviour. I tried calling setData recursivly from setData for each parent/child, but this leads to program crash.

I'm using qt5.

1
Not sure but I think you're just looking for the dataChanged signal.G.M.
From the docs " If the items do not have the same parent, the behavior is undefined". Therefore I don't think that I can use it in this caseHatatister
Right, so you need to emit the signal for each item that's changed. If that isn't what you're looking for then please edit your question to show some code -- preferably a minimal reproducible example.G.M.
You're right. Meanwhile I solved the problem with dataChanged. I called dataChanged with the wrong arguments by accident. I'm using pyqt and pyqt shadowed the resulting exception, so that it looked like a program crash ...Hatatister

1 Answers

2
votes

Pretty sure that what you're looking for is "rowsInserted" and the methods that relate to it. The "dataChanged" signal indicates that a given cell (or range) has changed values; it's not about changing table structure.

What you're doing here is removing and inserting rows as you move entries from one parent to another. You need to implement all of the methods related to that. There's also a "rowsMoved" that may better suit your needs.