[PySide] Bug is worse (was: QObject.destroyed() is not emitted)

Christian Tismer tismer at stackless.com
Thu Nov 29 23:40:11 CET 2012


Hi friends,

On 11/29/12 6:37 PM, Stephan Deibel wrote:
> Alexey Vihorev wrote:
>> Thanks, nice find, but... I hit the next hurdle trying to go this 
>> way. The signal QObject.destroyed(obj) is passing no arguments 
>> (probably because obj is already destroyed), so my static method has 
>> nothing to work on. Which kind of devaluates the whole idea, IMHO. 
>> And in PyQt4 it*does* pass the object. Even more: in PyQt4 there is 
>> no need for static method approach, as it works perfectly with 
>> instance methods:
>
> Yea, you would have to bind the necessary data to the callback like this:
>
>   def on_destroy(val1=self.whatever, val2=self.something):
>     print 'destroyed'
>   self.destroyed.connect(on_destroy)
>
> Whether this is possible or useful in your case is of course going to 
> depend on the details of the code.
>
> Having 'destroyed' be emitted before the object is destroyed and 
> getting the object reference as an arg makes more sense to me. That is 
> what PyQt seems to do and it is what QObject does under Qt using C++, 
> so I'd call this a bug in PySide.  I've added 
> https://bugreports.qt-project.org/browse/PYSIDE-129

This bug is only half of the story:

We had that simple @staticmethod work-around.
But actually, the main reason seems to be that PySide suffers
any reference cycle.

The tiny example again breaks as soon as I add a property:

{code}
     from PySide.QtCore import QAbstractTableModel, QObject

     class MyModel(QAbstractTableModel):
         def __init__(self, *args):
             super(MyModel, self).__init__(*args)
             self.destroyed.connect(self.onDestroy)

         @staticmethod
         def onDestroy():
             print('destroyed')

         @property
         def hugo(self):
             return 42

     m = MyModel()
     del m
{code}

Remove the @property, and it works, again.

Rule of thumb:
If you need anything like a property, use a normal Python class
and put your Qt object into it as an attribute. Otherwise your
program will tend to grow in memory ;-)

Stefan, I added this code to your bug report.

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Software Consulting          :     Have a break! Take a ride on Python's
Karl-Liebknecht-Str. 121     :    *Starship* http://starship.python.net/
14482 Potsdam                :     PGP key -> http://pgp.uni-mainz.de
phone +49 173 24 18 776  fax +49 (30) 700143-0023
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
       whom do you want to sponsor today?   http://www.stackless.com/




More information about the PySide mailing list