[PyKDE] Inheritance not supported?

Phil Thompson phil at river-bank.demon.co.uk
Sat Jan 25 14:03:00 GMT 2003

On Thursday 23 January 2003 1:52 pm, marvelan L wrote:
> Hi,
> I'm trying to use my own subclassed QPixmap in a QComboBox. But it
> seems that the QComboBox.pixmap method just returns the base class
> and not my sub class!
> Does not PyQt support the use of subclassed Qt objects?

Yes it does.

> This seems like a bug in PyQt to me?!?

I think it's a feature of Qt and you would observe the same behaviour with the 
equivalent C++ code.

> An example... The following should print MyPixmap and not qt.QPixmap:
> from qt import *
> import sys
> class MyPixmap(QPixmap):
>     def __init__(self):
>         QPixmap.__init__(self)
> app = QApplication(sys.argv)
> main = QMainWindow()
> main.setGeometry(0,0,400,400)
> app.setMainWidget(main)
> combo = QComboBox(main)
> combo.insertItem( MyPixmap(), "One")
> mypixmap = combo.pixmap(0)
> print "MyPixmap?", mypixmap, mypixmap.__class__
> main.show()
> app.exec_loop()

I haven't dug into the implementation of QComboBox but I think it makes a copy 
of the QPixmap so QComboBox.pixmap() is actually returning a different 

An indication that this is the case is that your MyPixmap instance will get 
garbage collected after the call to QComboBox.insertItem() because you do not 
keep a reference to it. If a copy wasn't being made then the QPixmap returned 
by QComboBox.pixmap() would be garbage and likely to cause a segmentation 
fault at some point.


More information about the PyQt mailing list