D-Bus python PyQt5服务实例

问题描述 投票:0回答:1

我正在尝试建立一个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浏览对象,我只看到这些接口。

  • org.freedesktop.DBus.Introspectable
  • org.freedesktop.DBus.Peer
  • org.freedesktop.DBus.Properties。

我缺少什么?

谢谢你

python service pyqt5 dbus
1个回答
1
投票

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

作为 文件说明:

[...]通过将QDBusAbstractAdaptor派生的一个或多个类附加到一个正常的QObject上,然后注册 用registerObject()对QObject进行修改。

改变 registerObject 参数,这样就可以了。

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

你还需要添加 result 参数到槽中,否则将无法工作。

    @pyqtSlot(result=str)
    def GetLastInput(self):
        return 'hello'
© www.soinside.com 2019 - 2024. All rights reserved.