Model/View - How to deal with insert/remove simulatinously.
-
Hello,
I have a view that shows a QTreeModel.
There is one action that makes something like this- parent
- child
- child
- child
is converted to:
- parent
- child
- child
- parent
- child
- child
The conversion happens inside a lib, so i cannot split it into two actions.
Now my question is how to call beginInsertRows() / endInsertRows() beginRemoveRows() / endRemoveRows() here, since it happens both at once.Can i cascade them like
beginRemoveRows() beginInsertRows() //do the action endInsertRows() endRemoveRows()
or what would be the right way to deal with this?
- parent
-
It's sort of an abuse of the API, but you could delay the notifications for after the change has occurred (assuming it's happening synchronously):
// Do the action without a care in the world beginRemoveRows() endRemoveRows() beginInsertRows() endInsertRows()
As for your question, I don't believe it's possible to nest these calls the way you want to ...
-
@kshegunov said in Model/View - How to deal with insert/remove simulatinously.:
but you could delay the notifications for after the change has occurred (assuming it's happening synchronously)
This will not work as begin() accesses the model and assumes the old structure.
The only way is to fix the library. When it changes the model it also must emit the signals.
-
@Christian-Ehrlicher said in Model/View - How to deal with insert/remove simulatinously.:
This will not work as begin() accesses the model and assumes the old structure.
Ah, the second
begin**
you mean ... yes, indeed,that'sthat may be a problem.
Well there's alwaysbeginResetModel
andendResetModel
if nothing else helps ... -
Thanks for the quick replies, The problem is, that the library does not have anything to do with Qt at all, so I cannot emit the signals there. (I have made a wrapper for that wraps the model in the lib inside a QAbstractItemModel).
Also "fixing" the lib does not really make sense, since the action that is performed is something like splitting an element into two parts which could maybe somehow be seperated into two actions but is not really what you would expect in the interface.
Resetting the model always has the disadvantage that the whole tree expansion state will collapse, which is kind of annoying.
I'll think a bit further about it and probably need to come up with some kind of dirty hack then.
-
I took a peek at the current implementation and from the quick glance it does seem your original idea may work. So I suggest you try it out. Here's a relevant piece of Qt's code:
https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN18QAbstractItemModel15beginRemoveRowsERK11QModelIndexii -
@kshegunov
Tried it, but unfortunately it crashes.
Thanks anyhow. I will go with resetting the model and maybe look for a solution some other time.
Its really not that much important.