problem with QtSignals "object has no attribute 'emit'"

N

News123

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
 
P

Phil Thompson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top