[PyQt] Bug in QTableView.setModel()

Aaron Digulla digulla at hepe.com
Mon Jan 7 10:12:10 GMT 2008

Quoting Andreas Pakulat <apaku at gmx.de>:

> On 06.01.08 21:13:44, Aaron Digulla wrote:
>> The root cause is that setModel() doesn't increment the usage counter of
>> the python class passed in, so Python will GC it if I don't save a
>> pointer elsewhere.
> Thats by purpose. One reason I can think of is that you often want to
> use the model elsewhere to insert data or do something with the data and
> then you often don't want a QAbstractItemModel instance but your
> specific subclass.

I'm talking about preventing the python class which is used as the  
model being garbage collected; it doesn't matter from which Qt class  
it is derived.

As it is, the class pointer is copied into the view class and then  
python frees the memory. That causes spurious display errors and  
crashes. No matter how you look at it, that's a bug.

I found another one but I'm not sure it can be fixed: Create a XML  
handler which derives from QXmlDefaultHandler and override  
startElement() like so:

     def __init__(self):
         self.path = []

     def startElement(self, namespaceURI, localName, qName, atts):

If you use qName later, the memory will have been freed (because Qt  
only gives you a read-only copy). Actually, if Qt was reusing that  
QString, the value could even change unexpectedly.

The workaround is to use self.path.append(unicode(qName)) to create a  
python string but that's error prone.


Aaron "Optimizer" Digulla a.k.a. Philmann Dark
"It's not the universe that's limited, it's our imagination.
Follow me and I'll show you something beyond the limits."

More information about the PyQt mailing list