[QGIS-Developer] Python slots don't get called when they're instance methods

G. Allegri giohappy at gmail.com
Wed Nov 28 15:32:11 PST 2018


During the porting of a plugin to QGIS 3.x I'm facing the following when
using Qgs* objects' signals (it doesn't happen with Qt widgets, etc.).
I'm using QGIS 3.4 on Window 10.

If I have a Python class like the following:

>class Foo():
>  def __init__(self):
>    QgsProject.instance().writeProject.connect(self.written)
>
>  def written(self, dom):
>    (do something)

self.written is never called. The signal can only connected to unbound
methods, e.g. to a top level def function or a class method. If I write:

>class Foo():
>  def __init__(self):
>    QgsProject.instance().writeProject.connect(Foo.written)
>
>  def written(dom):
>    (do something)

it works, but in general I cannot make make all my methods class methods of
course!

I've also tried making Foo derive from QObject. I've also tried decorating
"def writter" as a pyQtSlot, but nothing worked.

Is it only me, is it expected or is it a (known) bug?
In case it's the expected behaviour I wonder I the old classe signals like
I  used to write:

>QObject.connect(QgsProject.instance(),SIGNAL('writeProject (QDomDocument
&)'),self.written)

could be ported to QGIS 3

Cheers,
Giovanni
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20181129/6ec71555/attachment.html>


More information about the QGIS-Developer mailing list