首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未收到QDBus信号

未收到QDBus信号
EN

Stack Overflow用户
提问于 2018-01-19 01:54:44
回答 1查看 1.2K关注 0票数 0

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

receiver.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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中列出了信号发射:

代码语言:javascript
复制
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调试中列出了signal emit:

代码语言:javascript
复制
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=() )
EN

回答 1

Stack Overflow用户

发布于 2018-01-19 02:16:22

试着使用下面的代码。并检查你的接收器是否在信号发射时保持存活。

代码语言:javascript
复制
QDBusInterface *iface = new QDBusInterface(serviceName, servicePath, serviceInterface, QDBusConnection::sessionBus(), this);
if(iface->isValid()) {
    connect(iface, SIGNAL(PowerButtonPushed()), recv, SLOT(receiveSlot()));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48327534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档