Python中的QDBusInterface Upower属性为None

2024-09-29 23:33:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一些c++/qt代码转换为Python/qt

在c++中使用QDBusInterface:

auto qi = make_unique<QDBusInterface>("org.freedesktop.UPower", /org/freedesktop/UPower, "org.freedesktop.UPower", QDBusConnection::systemBus());

此时,我已经可以从其属性中读取值:

   auto prop = qi.property("OnBattery");

不幸的是,在Python中执行相同操作时,它不能以这种方式工作:

qi = QDBusInterface(serviceObject, path, interface, QDBusConnection.systemBus())

qi.isValid()返回True,但正在读取属性:

onBattery = qi.property("OnBattery")

返回无

也调用枚举设备的方法,既在C++和Python

上工作

有没有办法让它发挥作用


Tags: 代码orgautomake属性propertyqtunique
2条回答

这里的问题是缺乏好的例子:

from PyQt5.QtDBus import QDBusConnection, QDBusInterface, QDBusMessage

# DBus connection and Interface.
dbus_service = 'org.freedesktop.UPower'
dbus_path = '/org/freedesktop/UPower'
interface = QDBusInterface(dbus_service, dbus_path, 'org.freedesktop.DBus.Properties', QDBusConnection.systemBus())

# Get all properties.
msg = interface.call('GetAll', 'org.freedesktop.UPower')
print(msg.arguments())
for k, v in msg.arguments()[0].items():
    print('{}: {}'.format(k, v))

# Get just the requested property.
msg = interface.call('Get', 'org.freedesktop.UPower', 'OnBattery')
print(msg.arguments()[0])

我自己找到了间接的解决办法

它不再使用QDBusInterface,转而使用Python的dbus,可以像这样使用哪个API link

相关问题 更多 >

    热门问题