Returning struct with Qt DBus?

Florian Bruhin me at the-compiler.org
Thu Aug 18 10:14:41 BST 2022


Hey,

On Wed, Mar 24, 2021 at 06:31:19PM +0100, Florian Bruhin wrote:
> I'm currently trying to write a simple DBus notification server for end
> to end tests.
> 
> If I run the attached server (after killing my usual notification
> daemon), invoking e.g. GetCapabilities (which returns a list of strings)
> works fine:
> 
>     $ dbus-send --session --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.GetCapabilities
>     method return time=1616606628.700096 sender=:1.451 -> destination=:1.457 serial=16 reply_serial=2
>        array [
>           string "actions"
>        ]
> 
> However, I can't figure out how to get GetServerInformation to work,
> which is supposed to return a struct of four strings ("ssss" as DBus
> signature). I've tried returning a QDBusArgument and using:
> 
>     @pyqtSlot(QDBusMessage, result="QDBusArgument")
>     def GetServerInformation(self, message):
>         arg = QDBusArgument()
>         arg.beginStructure()
>         arg.add("qutebrowser")
>         arg.add("notification test server")
>         arg.add("v13.37")
>         arg.add("1.2")
>         arg.endStructure()
>         return arg
> 
> But that doesn't seem to work:
> 
>     $ dbus-send --session --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.GetServerInformation
>     Error org.freedesktop.DBus.Error.UnknownMethod: No such method 'GetServerInformation' in interface 'org.freedesktop.Notifications' at object path '/org/freedesktop/Notifications' (signature '')
> 
> If possible, I'd prefer using Qt DBus only rather than the python-dbus
> integration.
> 
> Any idea how I can get this to work? Or is this just not possible
> without being able to register Qt metatypes or something?

I finally have something that works - turns out that the solution is to
not use the return value at all, and instead send a reply to the bus
manually:

    @pyqtSlot(QDBusMessage)
    def GetServerInformation(self, msg: QDBusMessage):
        QDBusConnection.sessionBus().send(msg.createReply(["name", "vendor", "v0.0.1", "1.2"]))

Huge thanks to u/SomethingOfAGirl on Reddit, who contacted me with the same
problem (and later solution).

Florian

-- 
            me at the-compiler.org | https://www.qutebrowser.org 
       https://bruhin.software/ | https://github.com/sponsors/The-Compiler/
       GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
             I love long mails! | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20220818/488e82a3/attachment.sig>


More information about the PyQt mailing list