[PyQt] Segfault in toPyObject with QVariant and QObject (4.5.4)

Phil Thompson phil at riverbankcomputing.com
Wed Sep 16 15:16:33 BST 2009


On Tue, 15 Sep 2009 17:57:44 +0200, Florian Reinhard
<florian.reinhard at googlemail.com> wrote:
> This testcase causes a segfault in 4.5.4 works in 4.4.4
> 
> from PyQt4.QtCore import QVariant, QObject
> 
> class Test (QObject):
>     def __init__ (self,parent=None):
>         super(Test,self).__init__ (parent)
>         self.foo = 'bar'
> 
>     def uuu (self):
>         print self.foo
> 
> class Wrapper (QObject):
>     def __init__ (self,parent=None):
>         super(Wrapper,self).__init__ (parent)
>         test = Test ()
>         self.test = QVariant (test)
> 
>     def aaa (self):
>         self.test.toPyObject ().uuu ()
> 
> foo = Wrapper ()
> foo.aaa()

Unfortunately it's the correct behaviour - or the least incorrect behaviour
anyway.

You need to keep a reference to the "test" object - QVariant won't do it
for you.

In previous versions QVariant wrapped the object in something that
maintained the reference count. However that meant (for QObject
sub-classes) it wasn't recognised as a QObject and so couldn't be used by
JavaScript etc.

I've updated the documentation to cover this. It only affects QObject
sub-classes.

Phil


More information about the PyQt mailing list