[PyQt] Shouldn't an item view keep a reference to its associated model?

Phil Thompson phil at riverbankcomputing.co.uk
Fri Jul 20 08:48:20 BST 2007


On Friday 20 July 2007 1:37 am, Florent Rougon wrote:
> Hello,
>
> I had a hard time figuring out why my QAbstractItemModel subclass wasn't
> working properly. I added debug statements everywhere, which showed that
> my subclass was behaving fine. The only symptoms were:
>
>   1. The associated QTreeView() had an empty display.
>
>   2. My model was only queried for the columnCount() of the root item
>      (twice) and for part of the headerData(), and that's all. No
>      rowCount(), no index(), no data(), nothing.
>
> I reduced the problem to a minimal example, and eventually found that it
> was caused by the model (subclass of QAbstractItemModel) falling off the
> scope, and therefore presumably gargabe-collected by Python. If I keep
> an explicit reference to the model, everything works fine.
>
> My question is: is this behavior normal?

Yes, it reflects the Qt behaviour. Models are often shared between views and 
so (by default) aren't owned by any particular view. If you know there will 
only ever be one view then make it the parent of the model.

Phil


More information about the PyQt mailing list