[PyKDE] subclassing in pyqt

David Berner berner at ics.uci.edu
Sat Dec 23 22:12:05 GMT 2000


> On Fri, 22 Dec 2000, David Berner wrote:
>
> > i found a problem while subclassing pyqt-classes in python.
> >
> > e.g.
> >
> > >>> class Mylistview(QListView):
> > >>>   (...)
> > >>>
> > >>> class Mylistitem(QListViewItem):
> > >>>   def __init__(self, parent, name, myvar):
> > >>>     apply(QListViewItem.__init__,(self,parent,name)
> > >>>     self.MyVar = myvar
> > >>>     (...)
> > >>>
> > >>> a = Mylistview(parent)
> > >>> Mylistitem(a, "first item", 5)
> >
> > the problem is, when i call a method of Mylistview which returns an item
> > like:
> >
> > >>> x = a.selectedItem()
> >
> > since type(x) is not Mylistitem but QListViewItem, i can not
> access MyVar:
> >
> > >>> print x.MyVar
> > >>> (...) AttributeError: MyVar
> >
> > AFAIK there are no type-conversions in python (in C a simple
> cast would do),
> > so i see no way to solve this.
> >
> > all comments are welcome,
> > David
> >

On Saturday, December 23 Boudewijn Rempt wrote:

> This problem has been bugging me for some time, and I even have a set
> of standard tests to determine whether a current version of PyQt works
> or not - in the past there has been a version where it was impossible
> to get at the subclass.
>
> However, with current versions this is not a problem. The issue
> is caused by the fact that a Python subclass of a Qt class when
> instatiated creates two objects: a Python object and a Qt C++
> object.
>
> All you have to do is to keep a reference in Python to your subclass: if
> you let the Python reference to the object go out of scope, you lose your
> Python class. Qt still keeps a reference to the Qt object, i.e. to the
> QListViewItem, so that's what you'll get if you access it. Try the
> attached two scripts and see the essential difference in output:
>
> boud at calcifer:~/src/python/prj/test/inherit > python inherit.py
> <__main__.myListViewItem instance at 0x8242e3c>
> <__main__.myListViewItem instance at 0x8242e3c>
> slotHook
>
> boud at calcifer:~/src/python/prj/test/inherit > python inherit4.py
> <qt.QListViewItem instance at 0x8320b04>
> Traceback (most recent call last):
>   File "inherit4.py", line 39, in slotOpenFile
>     item.slotHook()
> AttributeError: slotHook
>
> Boudewijn Rempt

Yep, this was exactly the problem. I just didn't know that.
you helped me a lot, thanks.

David





More information about the PyQt mailing list