给定一个 pyqtBoundSignal 如何确定插槽?

2022-01-12 00:00:00 python debugging pyqt pyqt5 signals-slots

问题描述

给定信号,我如何确定特定信号连接到的插槽?

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.

我正在使用 pyqt5,python 2.7

I am using pyqt5, python 2.7


解决方案

没有the"槽这个东西,因为一个信号可以连接多个槽,或者多个其他信号,或者同一个信号/槽多个次.但无论如何,没有内置的 API 可以列出所有当前的连接.

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)

还有 connectNotify 和 disconnectNotify - 但这并没有让你走得更远,因为重新实现这些方法只会告诉您对象的哪些信号已连接或断开.

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.

QtTest 模块有 QSignalSpy(仅在 PyQt5 中可用),它可以记录给定信号的所有发射.但是,同样,这只会给你发送的参数,而不是它们被发送到的地方.

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.

我想 Qt 不为此提供 API 的原因是连接项的列表可能非常不稳定.

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:

我忘了提到 dumpObjectInfo,它显然做了什么您要求,但只能与 Qt 的调试版本一起使用 - 对于发布版本,它什么也不做.

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.

相关文章