From: News123 on
Hi,

I'm having a rather small code snippet, where I create pyQT signals.
I manage creating a signal as class attribute,
but I can't create a list of signals or a signal
as object.member.


> from PyQt4.QtGui import *
> from PyQt4.QtCore import *
>
> class MyWin(QMainWindow):
> clssig = pyqtSignal()
> sigarr = [ pyqtSignal() ]
> def emit_them(self):
> self.objsig = pyqtSignal()
> self.clssig.emit() # works
> self.sigarr[0].emit() # fails
> self.objsig.emit() # fails
>
> if __name__ == "__main__":
> app = QApplication(sys.argv)
> win = MyWin()
> win.show()
> win.emit_them()
> sys.exit(app.exec_())

The two lines marked with fails will fail with following error:
> AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'emit'

The QT documentation states:
"New signals should only be defined in sub-classes of QObject."

I guess, that his is the reason. though I don't know enough about PyQT
to understand the magic behind.


Now my question:

How could I create an array of signals if I wished to?

I can work aroud it, but would be curious.


Thanks for shadng some light on this (for me surprising) issue.


N
From: Phil Thompson on
On Sun, 14 Feb 2010 13:17:40 +0100, News123 <news123(a)free.fr> wrote:
> Hi,
>
> I'm having a rather small code snippet, where I create pyQT signals.
> I manage creating a signal as class attribute,
> but I can't create a list of signals or a signal
> as object.member.
>
>
>> from PyQt4.QtGui import *
>> from PyQt4.QtCore import *
>>
>> class MyWin(QMainWindow):
>> clssig = pyqtSignal()
>> sigarr = [ pyqtSignal() ]
>> def emit_them(self):
>> self.objsig = pyqtSignal()
>> self.clssig.emit() # works
>> self.sigarr[0].emit() # fails
>> self.objsig.emit() # fails
>>
>> if __name__ == "__main__":
>> app = QApplication(sys.argv)
>> win = MyWin()
>> win.show()
>> win.emit_them()
>> sys.exit(app.exec_())
>
> The two lines marked with fails will fail with following error:
>> AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'emit'
>
> The QT documentation states:
> "New signals should only be defined in sub-classes of QObject."
>
> I guess, that his is the reason. though I don't know enough about PyQT
> to understand the magic behind.
>
>
> Now my question:
>
> How could I create an array of signals if I wished to?
>
> I can work aroud it, but would be curious.
>
>
> Thanks for shadng some light on this (for me surprising) issue.

You can't create an array of signals.

Signals are defined to both Python and C++. This is done when the class is
defined by introspecting the class attributes - but it doesn't look for
signals any deeper than that, i.e. it won't look into your sigarr list.

Even if it did, there is a second issue. Signals have unbound and bound
versions (much like unbound and bound methods). The class attribute is an
unbound signal that is a descriptor that will return the bound signal. It
is the bound signal that implements the emit() method. It would be possible
for an unbound signal's __call__ method to also return a bound signal so
that you could do something like...

self.sigarr[0](self).emit()

....but I can't think of a valid use case.

Phil