[PyQt] Passsing Unsigned Int to DBus
Phil Thompson
phil at riverbankcomputing.com
Sun Feb 10 18:19:22 GMT 2019
On 10 Feb 2019, at 5:55 pm, Bo Møller Petersen <kemibo at gmail.com> wrote:
>
> I'm trying to use PyQt5's DBus module to interact with the KDE PowerManagerAgent on a linux system. When calling the AddInhibition method I need to send the first paramter as an uint32 (Unsigned int), but the code sends the value as a singed int.
>
> The code is written using Python 3 and using Pyqt5
>
> class PowerChangeInhibitor:
> def __init__(self):
> self.dBusService = 'org.kde.Solid.PowerManagement.PolicyAgent'
> self.dBusPath = '/org/kde/Solid/PowerManagement/PolicyAgent'
> self.dBusInterface = 'org.kde.Solid.PowerManagement.PolicyAgent'
> self.cookies = []
>
> def ActivateInhibition(self, interruptSession, changeProfile, changeScreenSettings,who, reason):
>
>
> self.dBus = QtDBus.QDBusConnection.sessionBus()
> if interruptSession:
> #dBusInterface = QtDBus.QDBusInterface(self.dBusService, self.dBusPath,'' , self.dBus)
> #reply = dBusInterface.call('AddInhibition',1,who,reason)
>
> msg = QtDBus.QDBusMessage.createMethodCall(self.dBusService, self.dBusPath,self.dBusInterface,'AddInhibition')
>
> #unsignedValue = 1 # writes 1 but typed as int32
> #unsignedValue = ctypes.c_uint(1) # Type not registered
> #unsignedValue = struct.unpack_from("I", struct.pack("i", 1)) # Type not registered
> #unsignedValue = QtCore.QByteArray(b'0x01')[0] # Type not registered
> #unsignedValue = bytes(1) # Type not registered
> #unsignedValue = QtDBus.QDBusVariant(1) # array of bytes "0x01"
> #unsignedValue = QtDBus.QDBusVariant(1)[0] # QDBusVariant does not support indexing
> #unsignedValue = QtDBus.QDBusVariant().setVariant(ctypes.c_uint(1)) # Variant contained QVariantInvalid
> unsignedValue = QtDBus.QDBusVariant(ctypes.c_uint(1)) # Type not registered
>
> msg << unsignedValue << who << reason
> reply = QtDBus.QDBusReply(self.dBus.call(msg))
> if reply.isValid():
> self.cookies.append(reply.value())
>
>
> I have commented out previous attempts with their results on the right. Using dbus-monitor I can see that the part where i'm struggeling is getting QDBusInterface to emit the desired UNIT32 type info.
>
> Any pointers on how I pass an unsigned int as an argument to my QDBusMessage ?
Use QVariant.convert()...
qvar = QVariant(ctypes.c_uint(1))
qvar.convert(QVariant.UInt)
unsignedValue = QtDBus.QDBusVariant(qvar)
Phil
More information about the PyQt
mailing list