Given a pyqtBoundSignal how to determine the slot?

2019-02-24 06:21发布

问题:

Given the signal, how can I determine the slot that a particular signal is connected to?

I am familiar with how to connect signal and slots, this is more for debugging purposes.

I am using pyqt5, python 2.7

回答1:

There is no such thing as "the" slot, because a signal can be connected to multiple slots, or multiple other signals, or the same signal/slot multiple times. But in any case, there is no built-in API that can list all the current connections.

You can get a count of the current connections for a signal, like this:

    count = button.receivers(button.clicked)

There is also connectNotify and disconnectNotify - but that doesn't get you much further, because reimplementing those methods will only tell you which signals were connected or disconnected for the object.

The QtTest module has QSignalSpy (only available in PyQt5), which can record all the emissions of a given signal. But, again, that will only give you the arguments that were sent, rather than where they were sent to.

I suppose the reason why Qt doesn't provide an API for this is that the list of connected items is potentially very volatile.

PS:

I forgot to mention dumpObjectInfo, which apparently does what you're asking for, but will only work with a debug build of Qt - for release builds, it does nothing.



回答2:

No robust way of doing that: check out the accepted answer of Determine signals connected to a given slot in Qt and the more recent answer by Kuba on that page. The conclusion is that you are better off stating what problem you are trying to solve, maybe others will have solution that does not involve determining connections.