[PyQt] SIP: Retaining python class variables of derived C++ class

Sandro Mani manisandro at gmail.com
Mon Jan 7 16:32:50 GMT 2019


Hi

I have the following scenario: -----------

C++:

class AbstractItem {     AbstractItem(const QString& id);     virtual 
~AbstractItem(); };

class Pool {     void addItem(AbstractItem* item /Transfer/);     
AbstractItem* getItem(const QString& id); };

Python: class Item(AbstractItem):     def __init__(self, id):         
AbstractItem.__init__(self, id)         self.value = "value"     def 
getValue(self):         return self.value

pool = Pool() pool.addItem(Item("xyz")) # ... item = pool.getItem("xyz") 
print(item.getValue()) ### AttributeError: 'Item' object has no 
attribute 'value'

-----------

The issue is that, when I pass the Python-deriver Item instance to the 
pool, lose the reference to it, and then re-retreive it from the pool, 
the Item instance will have lost class variables set in the Item 
__init__ function. As I understand, this is expected, because SIP keeps 
a python part and a c++ part of each object, and the python part is 
garbage-collected when the variable gets out of scope. Is there a way to 
make the above logic work?

Thanks Sandro

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190107/96c881f0/attachment.html>


More information about the PyQt mailing list