如何将 "org.freedesktop.DBus.Properties" 的 "PropertiesChanged" 信号正确连接到带有 "QDbusConnection" 的 QT 中的插槽?

How to properly connect the "PropertiesChanged" Signal of "org.freedesktop.DBus.Properties" to a slot in QT with "QDbusConnection"?

我正在实现一个嵌入式设备的接口,它通过 dbus API.

公开其硬件配置

目前我正在计算对设备硬件 I/O-pins 的访问权限。虽然配置 reading/writing 这些引脚已经按预期工作,但我无法将这些输入引脚的 PropertiesChanged 信号正确连接到我的 qt 应用程序中的插槽。该信号通知听众该引脚当前输入值的变化。

例如,当通过 dbus-monitor 发送信号时,信号如下所示:

signal sender=:1.2 -> dest=(null destination) serial=1023 path=/com/actia/platform/io/freq_in1_std; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
   string "com.actia.platform.io.digital_input"
   array [
      dict entry(
         string "InputValue"
         variant             int32 17857
      )
   ]
   array [
   ]

表示应用频率 1.7857 kHz 的值。

我这样连接信号:

    if (QDBusConnection::sessionBus().connect("com.actia.platform.io", "/com/actia/platform/io/freq_in1_std",
                "org.freedesktop.DBus.Properties", "PropertiesChanged", this,
                SLOT(propertyChanged(QString, QVariantMap, QStringList)))) {
        qDebug() << "PropertiesChanged signal connected successfully to slot";
        } else {
            qDebug() << "PropertiesChanged signal connection was not successful";
        }

连接的插槽如下所示:

void MyClass::propertyChanged(QString name, QVariantMap map, QStringList list)
    {
        qDebug() << QString("properties of interface %1 changed").arg(name);
        for (QVariantMap::const_iterator it = map.cbegin(), end = map.cend(); it != end; ++it) {
            qDebug() << "property: " << it.key() << " value: " << it.value();
        }
        for (const auto& element : list) {
            qDebug() << "list element: " << element;
        }
    }

我看到连接已成功完成,但显然签名似乎有问题,我想。因为我从来没有看到那个插槽被调用。

这是一个小示例应用程序 class 的 QDBUS_DEBUG=1 调试输出的摘录,我在其中实例化了一个输入和一个输出引脚,并在其中连接了 PropertiesChanged 信号,然后手动访问一些 dbus 方法和属性,然后让它空闲以等待信号捕获。我评论了一下调试日志。一段时间后,当 dbus 上的信号被触发几次时,我退出了应用程序。

### application.debug: start of initialization before issuing QCoreApplication::exec()...
### application.debug: creating a digital out pin...
// comment on the logs: The folloiwing 6 lines of debug output originate of instanciating the matching dbus proxy that has been generated by qdbusxml2cpp
QDBusConnectionPrivate(0x747021d0) : connected successfully
QDBusConnectionPrivate(0x747021d0) got message (signal): QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.1464") )
QDBusConnectionPrivate(0x747021d0) delivery is suspended
QDBusConnectionPrivate(0x747021d0) Adding rule: "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='com.actia.platform.io'"
QDBusConnectionPrivate(0x747021d0) sending message: QDBusMessage(type=MethodCall, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="GetNameOwner", signature="", contents=("com.actia.platform.io") )
QDBusConnectionPrivate(0x747021d0) got message reply: QDBusMessage(type=MethodReturn, service="org.freedesktop.DBus", signature="s", contents=(":1.2") )
### application.debug: Connecting the PropertiesChanged signal for the created pin...
QDBusConnectionPrivate(0x74710460) : connected successfully
QDBusConnectionPrivate(0x74710460) got message (signal): QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.28") )
QDBusConnectionPrivate(0x74710460) delivery is suspended
QDBusConnectionPrivate(0x74710460) Adding rule: "type='signal',sender='com.actia.platform.io',path='/com/actia/platform/io/dout1',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"
QDBusConnectionPrivate(0x74710460) sending message: QDBusMessage(type=MethodCall, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="GetNameOwner", signature="", contents=("com.actia.platform.io") )
QDBusConnectionPrivate(0x74710460) got message reply: QDBusMessage(type=Error, service="org.freedesktop.DBus", error name="org.freedesktop.DBus.Error.NameHasNoOwner", error message="Could not get owner of name 'com.actia.platform.io': no such name", signature="s", contents=("Could not get owner of name 'com.actia.platform.io': no such name") )
QDBusConnectionPrivate(0x74710460) Watching service "com.actia.platform.io" for owner changes (current owner: "" )
### application.debug: PropertiesChanged signal connected successfully to slot
### application.debug: creating a digital in pin...
// comment on the logs: The following 2 lines of debug output originate of instanciating the matching dbus proxy that has been generated by qdbusxml2cpp
QDBusConnectionPrivate(0x747021d0) sending message: QDBusMessage(type=MethodCall, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="GetNameOwner", signature="", contents=("com.actia.platform.io") )
QDBusConnectionPrivate(0x747021d0) got message reply: QDBusMessage(type=MethodReturn, service="org.freedesktop.DBus", signature="s", contents=(":1.2") )
### application.debug: Connecting the PropertiesChanged signal for the created pin...
QDBusConnectionPrivate(0x74710460) Adding rule: "type='signal',sender='com.actia.platform.io',path='/com/actia/platform/io/freq_in1_std',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"
### application.debug: PropertiesChanged signal connected successfully to slot
### application.debug: success setting pin modes for the digital out pin to pwm generation:  true
// comment on the logs: setting a value on an output pin now
QDBusConnectionPrivate(0x747021d0) sending message: QDBusMessage(type=MethodCall, service="com.actia.platform.io", path="/com/actia/platform/io/dout1", interface="com.actia.platform.io.digital_output", member="SetValue", signature="", contents=(200, 80) )
QDBusConnectionPrivate(0x747021d0) got message reply: QDBusMessage(type=MethodReturn, service=":1.2", signature="", contents=() )
### application.debug: success setting pwm of 20 Hz at 80% duty cycle:  true
// comment on the logs: reading back the properties for the pins to verify
QDBusConnectionPrivate(0x747021d0) sending message: QDBusMessage(type=MethodCall, service="com.actia.platform.io", path="/com/actia/platform/io/dout1", interface="org.freedesktop.DBus.Properties", member="Get", signature="", contents=("com.actia.platform.io.digital_output", "OutputFrequency") )
QDBusConnectionPrivate(0x747021d0) got message reply: QDBusMessage(type=MethodReturn, service=":1.2", signature="v", contents=([Variant(int): 200]) )
QDBusConnectionPrivate(0x747021d0) sending message: QDBusMessage(type=MethodCall, service="com.actia.platform.io", path="/com/actia/platform/io/dout1", interface="org.freedesktop.DBus.Properties", member="Get", signature="", contents=("com.actia.platform.io.digital_output", "OutputRatio") )
QDBusConnectionPrivate(0x747021d0) got message reply: QDBusMessage(type=MethodReturn, service=":1.2", signature="v", contents=([Variant(int): 80]) )
### application.debug: pwm backread frequency 20, duty cycle 80
// comment on the logs: setting the mode of a digital input pin to read a frequency instead a logical 0 or 1
QDBusConnectionPrivate(0x747021d0) sending message: QDBusMessage(type=MethodCall, service="com.actia.platform.io", path="/com/actia/platform/io/freq_in1_std", interface="com.actia.platform.io.digital_input", member="SetInputType", signature="", contents=(2) )
QDBusConnectionPrivate(0x747021d0) got message reply: QDBusMessage(type=MethodReturn, service=":1.2", signature="", contents=() )
### application.debug: setting pin modes of the digital in pin to read a frequency:  true
// comment on the logs: reading the currently applied frequency from the pin now
QDBusConnectionPrivate(0x747021d0) sending message: QDBusMessage(type=MethodCall, service="com.actia.platform.io", path="/com/actia/platform/io/freq_in1_std", interface="org.freedesktop.DBus.Properties", member="Get", signature="", contents=("com.actia.platform.io.digital_input", "InputValue") )
QDBusConnectionPrivate(0x747021d0) got message reply: QDBusMessage(type=MethodReturn, service=":1.2", signature="v", contents=([Variant(int): 17361]) )
### application.debug: reading the current frequency from digital input:  1736.1  Hz
### application.debug: end of actual code during initialization, now issuing QCoreApplication::exec()...
QDBusConnectionPrivate(0x747021d0) dequeueing message QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.1464") )
QDBusConnectionPrivate(0x74710460) dequeueing message QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.28") )
[...] program running, waiting for signals
### application.debug: QCoreApplication::quit()
QDBusConnectionPrivate(0x74710460) Removing rule: "type='signal',sender='com.actia.platform.io',path='/com/actia/platform/io/freq_in1_std',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"
QDBusConnectionPrivate(0x74710460) Removing rule: "type='signal',sender='com.actia.platform.io',path='/com/actia/platform/io/dout1',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged'"
QDBusConnectionPrivate(0x747021d0) Disconnected
QDBusConnectionPrivate(0x74710460) Disconnected

我在 SO 或 QT 论坛上阅读了大量关于连接到 PropertiesChanged 信号的建议,并从那里尝试了一些建议但无济于事。一定有一些我不知道的签名不匹配。但是,我查看了一些 dbus 应用程序的代码,例如 networkmanager-qt,它们使用了我在签名中也使用的三个参数:QStringQVariantMapQStringList.

我现在被困住了,希望有人能给我另一个提示。 提前致谢。

嗯,我真的不知道为什么。但是,当我使用新的 from-source qt 构建(与以前相同的版本)再次尝试上面的确切代码时,它起作用了。所以我认为这个问题暂时解决了。