問題描述
給定信號,我如何確定特定信號連接到的插槽?
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.
這篇關于給定一個 pyqtBoundSignal 如何確定插槽?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!