【问题标题】:QDBusInterface Upower Properties in Python are NonePython 中的 QDBusInterface Upower 属性为无
【发布时间】:2020-05-17 01:10:16
【问题描述】:

我正在尝试将一些 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 上也可以调用 EnumerateDevices 等方法

有没有办法让它工作?

【问题讨论】:

    标签: python c++ qt properties


    【解决方案1】:

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

    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])
    

    【讨论】:

      【解决方案2】:

      我自己找到了间接解决方案。

      它放弃使用 QDBusInterface 并切换到 Python 的 dbus 可以像在这个 link 中使用的 API

      【讨论】:

        猜你喜欢
        • 2013-12-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        • 1970-01-01
        • 2014-06-19
        • 1970-01-01
        • 2020-10-07
        • 2012-08-11
        相关资源
        最近更新 更多