0

Using the pyside qt binding module inside a small python 2.7 project I want to find out the source of a signal. So inside a slot I want to have some means to ask by what signal this slot has actually been triggered with.

I figured out that this gives me a clean debug notation of the actual sender object:

sigItem = "<anonymous>" if not self.sender() else \
          re.search('<(.+) object at .+>', repr(self.sender()), 0).group(1)

But all I found until now to identify the actual signal that caused this slot to be called is apparently the signals index inside the caller object:

sigIndex = self.senderSignalIndex()

So how can I find out the actual signals name?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • AFAIK, a QT signal, just like other C++ method or variable, doesn't store its name. Therefore, there's no way to know, you can get a reference to a signal, but not its name. – Kien Truong Jan 31 '13 at 11:23
  • @Dikei Hm, ok, but... If I have a reference to the emitting object and the index of the signal raised - still there is no way to tell the name of the method indicated by the index? If something like a numerical index exists, I'd expect the index to be valid inside some list or dictionary... – arkascha Jan 31 '13 at 11:24

1 Answers1

3

You could use the index to get a QMetaMethod, but not much more. Apparently, Qt doesn't want you to know more.

from PyQt4 import QtCore

senderSignalId = None

class Sender(QtCore.QObject):

    signal1 = QtCore.pyqtSignal()
    signal2 = QtCore.pyqtSignal()

class Receiver(QtCore.QObject):

    @QtCore.pyqtSlot()
    def slot(self):
        global senderSignalId
        senderSignalId = self.senderSignalIndex()

sender = Sender()
receiver = Receiver()
sender.signal1.connect(receiver.slot)
sender.signal2.connect(receiver.slot)

sender.signal1.emit()
print sender.metaObject().method(senderSignalId).signature() // signal1()

sender.signal2.emit()
print sender.metaObject().method(senderSignalId).signature() // signal2()
Kien Truong
  • 11,179
  • 2
  • 30
  • 36