我想在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中列出了信号emition:
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=() )
答案 0 :(得分:0)
尝试使用此代码。并检查您的接收器在信号发射时保持活着状态。
QDBusInterface *iface = new QDBusInterface(serviceName, servicePath, serviceInterface, QDBusConnection::sessionBus(), this);
if(iface->isValid()) {
connect(iface, SIGNAL(PowerButtonPushed()), recv, SLOT(receiveSlot()));
}