[PyQt] adding signal from class method?

Erik Hvatum ice.rikh at gmail.com
Thu Aug 14 00:03:47 BST 2014

Hi everybody,

Is it possible to add a signal to a pyqt class from a class method
(decorated with @classmethod) or from outside of the class?  I am somewhat
surprised that the following does not work:

class C(Qt.QObject):
    def __init__(self):

C.fooSignal = Qt.pyqtSignal(int, name="fooSignal")
c = C()
!!!! AttributeError: 'C' does not have a signal with the signature

This arises because I'd like to programmatically generate and add a bunch
of signals to a class before instantiating it.  It can be done with exec
provided the exec call uses the default global and local dicts, leading me
to suspect that there's some kind of magic absolutely requiring the current
local and global namespaces to be in a very specific state.  For now, I
actually use this as a workaround by having my class method generate a
string that I then exec in the class definition, which is a bit ugly.

IPython transcript of test case:

> ipython
Python 3.4.1 (default, Jun 20 2014, 12:48:18)
Type "copyright", "credits" or "license" for more information.

IPython 3.0.0-dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
QML debugging is enabled. Only use this in a safe environment.

In [1]: class C(Qt.QObject):
   ...:     def __init__(self):
   ...:         super().__init__()
   ...:     exec('aSignal = Qt.pyqtSignal(bool)')
   ...:     @classmethod
   ...:     def make_bSignal(cls):
   ...:         exec('cls.bSignal = Qt.pyqtSignal(bool, name="bSignal")')

In [2]: C.make_bSignal()

In [3]: C.cSignal = Qt.pyqtSignal(bool, name="cSignal")

In [4]: c = C()

In [5]: c.dump
c.dumpObjectInfo  c.dumpObjectTree

In [5]: c.dumpObjectInfo()
OBJECT C::unnamed

In [6]: c.aSignal.emit(True)

In [7]: c.bSignal.emit(True)
AttributeError                            Traceback (most recent call last)
<ipython-input-7-116b5fc1d216> in <module>()
----> 1 c.bSignal.emit(True)

AttributeError: 'C' does not have a signal with the signature bSignal(bool)

In [8]: c.cSignal.emit(True)
AttributeError                            Traceback (most recent call last)
<ipython-input-8-c51ab45ecb54> in <module>()
----> 1 c.cSignal.emit(True)

AttributeError: 'C' does not have a signal with the signature cSignal(bool)

In [9]: quit()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140813/98131d48/attachment.html>

More information about the PyQt mailing list