【问题标题】:QDBus signal not received未收到 QDBus 信号
【发布时间】:2018-01-18 17:54:44
【问题描述】:

我想在 Qt 应用程序中接收 DBus 信号:

receiver.h

#ifndef RECEIVER_H
#define RECEIVER_H

#include <iostream>
#include <QObject>

class Receiver : public QObject {
    Q_OBJECT
public slots:
    void receive() {
        std::cout << "Received!";
    }
};

#endif // RECEIVER_H

main.cpp

#include <QCoreApplication>
#include <QDBusConnection>
#include "receiver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    auto recv = new Receiver;
    QDBusConnection::sessionBus().connect("com.ControllerBoard",
                                          "/com/ControllerBoard/Buttons",
                                          "com.ControllerBoard.Buttons",
                                          "PowerButtonPushed", recv, SLOT(receive()));
    return a.exec();
}

不幸的是,这不起作用,但是我在 dbus-monitor 中列出了信号发射:

signal time=1516297427.711775 sender=:1.89 -> destination=(null destination) serial=313 path=/com/ControllerBoard/Buttons; interface=com.ControllerBoard.Buttons; member=PowerButtonPushed
signal time=1516297427.807397 sender=:1.89 -> destination=(null destination) serial=314 path=/com/ControllerBoard/Buttons; interface=com.ControllerBoard.Buttons; member=PowerButtonReleased

编辑:

我尝试使用QDBusInterface,但它仍然无法正常工作。我检查了QDBusInterface::isValid() 和QDBusConnection::isConnected(),都返回true。

EDIT2:很奇怪,当我使用QDBUS_DEBUG=1 运行此应用程序时,我在 QDBus 调试中列出了信号发射:

QDBusConnectionPrivate(0x7fe7780032f0) : connected successfully
QDBusConnectionPrivate(0x7fe7780032f0) got message (signal): QDBusMessage(type=Signal, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="NameAcquired", signature="s", contents=(":1.104") )
QDBusConnectionPrivate(0x7fe7780032f0) sending message: QDBusMessage(type=MethodCall, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="GetNameOwner", signature="", contents=("com.ControllerBoard") )
QDBusConnectionPrivate(0x7fe7780032f0) got message reply: QDBusMessage(type=MethodReturn, service="org.freedesktop.DBus", signature="s", contents=(":1.98") )
QDBusConnectionPrivate(0x7fe7780032f0) sending message: QDBusMessage(type=MethodCall, service="com.ControllerBoard", path="/com/ControllerBoard/Buttons", interface="org.freedesktop.DBus.Introspectable", member="Introspect", signature="", contents=() )
QDBusConnectionPrivate(0x7fe7780032f0) got message reply: QDBusMessage(type=MethodReturn, service=":1.98", signature="s", contents=("<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
  <interface name="com.ControllerBoard.Buttons">
    <signal name="PowerButtonPushed"/>
    <signal name="PowerButtonReleased"/>
    <signal name="ScalesButtonPushed"/>
    <signal name="ScalesButtonReleased"/>
    <signal name="TimerButtonPushed"/>
    <signal name="TimerButtonReleased"/>
  </interface>
  <interface name="org.freedesktop.DBus.Properties">
    <method name="Get">
      <arg name="interface_name" type="s" direction="in"/>
      <arg name="property_name" type="s" direction="in"/>
      <arg name="value" type="v" direction="out"/>
    </method>
    <method name="Set">
      <arg name="interface_name" type="s" direction="in"/>
      <arg name="property_name" type="s" direction="in"/>
      <arg name="value" type="v" direction="in"/>
    </method>
    <method name="GetAll">
      <arg name="interface_name" type="s" direction="in"/>
      <arg name="values" type="a{sv}" direction="out"/>
      <annotation name="org.qtproject.QtDBus.QtTypeName.Out0" value="QVariantMap"/>
    </method>
    <signal name="PropertiesChanged">
      <arg name="interface_name" type="s" direction="out"/>
      <arg name="changed_properties" type="a{sv}" direction="out"/>
      <annotation name="org.qtproject.QtDBus.QtTypeName.Out1" value="QVariantMap"/>
      <arg name="invalidated_properties" type="as" direction="out"/>
    </signal>
  </interface>
  <interface name="org.freedesktop.DBus.Introspectable">
    <method name="Introspect">
      <arg name="xml_data" type="s" direction="out"/>
    </method>
  </interface>
  <interface name="org.freedesktop.DBus.Peer">
    <method name="Ping"/>
    <method name="GetMachineId">
      <arg name="machine_uuid" type="s" direction="out"/>
    </method>
  </interface>
</node>
") )
QDBusConnectionPrivate(0x7fe7780032f0) Adding rule: "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='NameOwnerChanged',arg0='com.ControllerBoard'"
QDBusConnectionPrivate(0x7fe7780032f0) Adding rule: "type='signal',sender='com.ControllerBoard',path='/com/ControllerBoard/Buttons',interface='com.ControllerBoard.Buttons',member='PowerButtonPushed'"
QDBusConnectionPrivate(0x7fe7780032f0) sending message: QDBusMessage(type=MethodCall, service="org.freedesktop.DBus", path="/org/freedesktop/DBus", interface="org.freedesktop.DBus", member="GetNameOwner", signature="", contents=("com.ControllerBoard") )
QDBusConnectionPrivate(0x7fe7780032f0) got message reply: QDBusMessage(type=MethodReturn, service="org.freedesktop.DBus", signature="s", contents=(":1.98") )
QDBusConnectionPrivate(0x7fe7780032f0) Watching service "com.ControllerBoard" for owner changes (current owner: ":1.98" )
QDBusConnectionPrivate(0x7fe7780032f0) got message (signal): QDBusMessage(type=Signal, service=":1.98", path="/com/ControllerBoard/Buttons", interface="com.ControllerBoard.Buttons", member="PowerButtonPushed", signature="", contents=() )

【问题讨论】:

  • 您不应该只在您的连接呼叫中使用SLOT(receive()) 吗?
  • @csunday95 对不起,这只是我的错误。固定。

标签: c++ qt dbus


【解决方案1】:

尝试使用此代码。并检查您的接收器在信号发射时是否保持活动状态。

QDBusInterface *iface = new QDBusInterface(serviceName, servicePath, serviceInterface, QDBusConnection::sessionBus(), this);
if(iface->isValid()) {
    connect(iface, SIGNAL(PowerButtonPushed()), recv, SLOT(receiveSlot()));
}

【讨论】:

    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2023-03-30
    • 2017-02-06
    相关资源
    最近更新 更多