I'm trying to put up a PyQt5 D-Bus service example.
Here's my code so far
#!/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_()
This runs without error but the 'org.example.chat' interface isn't exported
~$ 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 '/'
Browsing the / object with d-feet, I see these interfaces only:
What am I missing?
Thanks
The object to be registered cannot be the adaptor itself, but another QObject.
As the 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().
Change the registerObject
argument to the parent, and it will work:
def __init__(self, parent):
super().__init__(parent)
QDBusConnection.sessionBus().registerObject("/", parent)
You also need to add the result
argument to the slot, otherwise it won't work:
@pyqtSlot(result=str)
def GetLastInput(self):
return 'hello'