D-Bus python PyQt5 服务示例

D-Bus python PyQt5 service example

我正在尝试提供一个 PyQt5 D-Bus 服务示例。

到目前为止,这是我的代码

#!/usr/bin/python3
from PyQt5.QtCore import QCoreApplication, Q_CLASSINFO, pyqtSlot, QObject
from PyQt5.QtDBus import QDBusConnection, QDBusAbstractAdaptor

import signal
signal.signal(signal.SIGINT, signal.SIG_DFL)

class Service(QDBusAbstractAdaptor):
    Q_CLASSINFO('D-Bus Interface', 'org.example.chat')
    Q_CLASSINFO('D-Bus Introspection', ''
                '  <interface name="org.example.chat">\n'
                '    <method name="GetLastInput">\n'
                '      <arg direction="out" type="s" name="text"/>\n'
                '    </method>\n'
                '  </interface>\n'
                '')

    def __init__(self, parent):
        super().__init__(parent)
        QDBusConnection.sessionBus().registerObject("/", self)

        if not QDBusConnection.sessionBus().registerService("org.example.chat"):
            print(QDBusConnection.sessionBus().lastError().message())

    @pyqtSlot()
    def GetLastInput(self):
        return 'hello'


if __name__ == '__main__':
    app = QCoreApplication([])

    if not QDBusConnection.sessionBus().isConnected():
        print ("Cannot connect to the D-Bus session bus.\n"
               "Please check your system settings and try again.\n");

    service = Service(app)
    print ('Now we are running')
    app.exec_()

这运行没有错误,但 'org.example.chat' 接口未导出

~$ dbus-send --session --dest="org.example.chat" --type="method_call" --print-reply "/" "org.example.chat.GetLastInput"

Error org.freedesktop.DBus.Error.UnknownInterface: No such interface 'org.example.chat' at object path '/'

用d-feet浏览/对象,只看到这些界面:

我错过了什么?

谢谢

要注册的对象不能是适配器本身,而是另一个QObject。

作为 documentation explains:

[...] This is accomplished by attaching a one or more classes derived from QDBusAbstractAdaptor to a normal QObject and then registering that QObject with registerObject().

registerObject 参数更改为父级,它将起作用:

    def __init__(self, parent):
        super().__init__(parent)
        QDBusConnection.sessionBus().registerObject("/", parent)

你还需要在插槽中添加result参数,否则将不起作用:

    @pyqtSlot(<b>result=str</b>)
    def GetLastInput(self):
        return 'hello'