[PyQt] Problem with custom class derived from QObject as signal parameter

Phil Thompson phil at riverbankcomputing.com
Tue Jul 14 17:29:04 BST 2015


On 14/07/2015 12:01 pm, Johan Fläckman wrote:
> Hi, I have a C++ class that emits a pointer to a class deriving from
> QObject. The instance ends up in python slots as a QObject (and not to 
> the
> class deriving from it). Can someone help me figure out what I have 
> done
> wrong?
> 
> Python 2.7, 64bit
> Windows 7
> 
> Qt 5.4.1
> PyQt 5.4.1
> SIP 4.16.7
> 
> 
> I tried to make a minimal repro case;
> 
> 
> *C++*
> class Foo : public QObject
> {
>     Q_OBJECT
> public:
>     explicit Foo(QObject* parent = 0) : QObject(parent) { }
>     ~Foo() {};
> };
> 
> class Emitter : public QObject
> {
>     Q_OBJECT
> public:
>     Emitter() {}
>     ~Emitter() {}
>     void AddItem() { emit itemAdded( new Foo ); }
> 
> Q_SIGNALS:
>     void itemAdded(Foo*);
> };
> 
> *SIP*
> %Module(name=pyqt_test, call_super_init=True)
> 
> %ModuleCode
> #include <test.h>
> %End
> 
> %Import QtCore/QtCoremod.sip
> 
> class Foo : public QObject
> {
> %TypeHeaderCode
> #include <test.h>
> %End
> public:
>     explicit Foo(QObject *parent /TransferThis/ = 0);
>     ~Foo();
> };
> 
> class Emitter : public QObject
> {
> %TypeHeaderCode
> #include <test.h>
> %End
> public:
>     Emitter();
>     virtual ~Emitter();
>     void AddItem();
> 
> Q_SIGNALS:
>     void itemAdded(Foo*);
> };
> 
> 
> *Python*
> 
> from pyqt_test import Foo, Emitter
> 
> from PyQt5.QtCore import pyqtSlot
> 
> 
> @pyqtSlot(Foo)
> 
> def pyslot(inst):
> 
>     print inst
> 
> 
> em = Emitter()
> 
> em.itemAdded.connect(pyslot)
> 
> em.AddItem()
> 
> *Py output*
> 
> <PyQt5.QtCore.QObject object at 0x000000000542B558>
> 
> 
> 
> If I modify the example and use, for example, QFileSelector, instead of 
> the
> Foo class then it gets passed along as a QFileSelector to the python 
> slot.
> Leading me to believe I am missing some kind of directive or annotation 
> in
> my sip files.

%ConvertToSubClassCode

Phil


More information about the PyQt mailing list