PyQt 5.6:连接到 DBus 信号挂起

PyQt 5.6: connecting to a DBus signal hangs

我正在尝试将插槽连接到 PyQt 5.6 中通过 DBus 发出的信号 Python 3.5。

当我 运行 我的脚本像这样 QDBUS_DEBUG=1 python3 qtdbustest.py 它永远不会到达对 print('Connected') 的调用,而是挂在 bus.connect(...) 调用。总线上的信号在调试输出中很明显:

QDBusConnectionPrivate(0x7f3e60002b00) : connected successfully QDBusConnectionPrivate(0x7f3e60002b00) got message (signal): QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.137") ) QDBusConnectionPrivate(0x7f3e60002b00) delivery is suspended

这是我的最小工作示例:

#!/usr/bin/python3

import sys

from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtDBus import QDBusConnection, QDBusMessage


class DbusTest(QObject):

    def __init__(self):
        super(DbusTest, self).__init__()
        bus = QDBusConnection.systemBus()
        bus.connect(
            'org.freedesktop.DBus',
            '/org/freedesktop/DBus',
            'org.freedesktop.DBus',
            'NameAcquired',
            self.testMessage
        )
        print('Connected')

    @pyqtSlot(QDBusMessage)
    def testMessage(self, msg):
        print(msg)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    discoverer = DbusTest()
    sys.exit(app.exec_())

我做错了什么?一定有什么我忽略了,所以对 bus.connect(...) 的调用实际上是 returns.

我能够像这样修复你的例子:

    bus = QDBusConnection.systemBus()
    bus.registerObject('/', self)
    bus.connect( ...

但是,我不得不承认我并不完全理解它为什么起作用(也就是说,我找不到任何确凿的文档)。不过,在尝试建立连接之前需要注册接收器对象似乎是有道理的。