[PyQt] QTreeView access violation

Matt Newell newellm at blur.com
Fri Sep 14 18:11:23 BST 2007

On Thursday 13 September 2007 01:53, Dirk Wagener wrote:
> Hi
> I have the following problem when performing the following steps in
> sequence:
> * I have a QTreeView using a custom model.
> * I expand a node that contains a bunch of items.
> * I select one of the items (children) by clicking on it.
> * I then edit the name of the item in-place (custom setData()
> implementation).
> * I signal a dataChanged() event.
> * I send a signal from somewhere else that the parent must be refreshed.
> * The parent removes all its children (removeRows()) and re-populates.
> * CRASH!! (only sporadically)
>    Qt gives my one of these:
> I am not 100% sure about this, but it seems to have something to do with
> the fact that a selected item gets removed.
> If I call clearSelection() before removing the rows, the violation does
> not happen! This feels like a bit of a hack. What is the
> correct way to handle this?
> Kind regards
> Dirk

What Qt version are you using? This may be a known Qt bug that was fixed.  

Are you calling beginRemoveRows/endRemoveRows with the proper values?

It's best to avoid removing and re-adding rows if you can(assuming the rows 
that are re-added are the same contents), so that selections and state are 
preserved.  Instead just update the data of the changed rows, add new rows, 
and delete old rows.

It also sounds like you are removing the rows from inside the dataChanged 
signal, instead of letting the stack unwind first.  You could fire 
the "parent must be refreshed." signal using a single shot timer, that could 
fix the problem.


More information about the PyQt mailing list