【问题标题】:PyQt 5.6: connecting to a DBus signal hangsPyQt 5.6:连接到 DBus 信号挂起
【发布时间】:2016-11-03 17:05:34
【问题描述】:

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

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

QDBusConnectionPrivate(0x7f3e60002b00) : 连接成功 QDBusConnectionPrivate(0x7f3e60002b00) 收到消息(信号): QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.137") ) QDBusConnectionPrivate(0x7f3e60002b00) 交付暂停

这是我的最小工作示例:

#!/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(...) 的调用实际上会返回。

【问题讨论】:

    标签: python-3.x pyqt5 dbus


    【解决方案1】:

    我能够像这样修复您的示例:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-16
      • 2013-12-31
      • 2011-05-08
      • 2012-05-25
      • 2013-07-14
      • 2016-09-16
      • 1970-01-01
      • 2011-05-23
      相关资源
      最近更新 更多