Search code examples
c++qtqt4qtgui

How to sort with beginMoveRows without persistent index corruption / duplication?


I'm trying to use beginMoveRows / endMoveRows to make persistent indexes stick, but sometimes expanded state flags / persistent indexes are duplicated, where they should not be.

There is quite a lot of code, so I'll go through what I think I have told the machine to do:

There is a method, sortChildrenOf(item) which does all the magic.

  • Find children from item and call sortChildrenOf with each child as a parameter
  • save old order
  • quickSort children
  • find differences in old order and the new
  • for each difference:
  • beingMoveRows
  • apply change
  • endMoveRows

Everything works perfectly when there are 2 levels, but when I input a "long" tree of data, persistent indexes get corrupted.

The data in the tree is updated from network, but the actual update is done in the gui thread.

Is there some precise order I should do stuff in? Might I have forgotten to inherit some method that causes this?

I'v got these methods implemented: - data - flags - getItem - index - parent - setData

Edit: forgot to mention, i got emit layoutAboutToBeChanged and emit layoutChanged before and after the main sortChildrenOf call.


Solution

  • I got it to work, but not with beginMoveRows and endMoveRows. I used the old system of emitting layoutAboutToChange getting the list of persistent indexes manipulating that and setting it back with changePersistentIndexList and finally emitting layout changed.

    Since this was the fix, I'm lead to believe that there is some bug withtin beginMoveRows, endMoveRows and persistent indexes with tree type data.

    Ask if you need a better example of the code.