Hello! I’m trying to build a simple app that uses QDBusConnection to connect to the system dbus and listen for calls from other processes. I tried mixing up this guide and this SA question, and here’s the result:
main.cpp
#include <QCoreApplication>
#include <QDebug>
#include <QtDBus/QtDBus>
#include "background.h"
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
qWarning() << "daemon started!";
Background *e = new Background;
e->setupDBus();
qWarning() << "daemon initialized";
return app.exec();
}
background.h
#ifndef BACKGROUND_H
#define BACKGROUND_H
#include <QCoreApplication>
#include <QDebug>
#include <QtDBus/QtDBus>
#include <QObject>
class Background : QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.foo.Background")
public:
Background(){
}
void setupDBus()
{
QDBusConnection session = QDBusConnection("org.foo.Background");
if (!session.isConnected())
{
qWarning("Cannot connect to the D-Bus session bus.");
//return;
} else {
qWarning("successfully connected to the D-Bus session bus.");
}
session.connect("", "/", "org.foo.Background", "slot_foo", this, SLOT(slot_foo));
if(!session.registerObject("/", this, QDBusConnection::ExportScriptableContents)) {
qWarning ("Cannot registerObject 1.");
// return;
} else {
qWarning ("done registerObject 1.");
}
if(!session.registerService("org.foo.Background")) {
qWarning ("Cannot registerObject 2.");
// return;
} else {
qWarning ("done registerObject 2.");
}
if (!session.isConnected())
{
qWarning("Cannot connect to the D-Bus session bus.");
//return;
} else {
qWarning("successfully connected to the D-Bus session bus.");
}
}
Q_SIGNALS:
public Q_SLOTS:
Q_SCRIPTABLE void slot_foo()
{
qDebug() << "request received";
}
protected Q_SLOTS:
void dbusCanNotSeeMe(){}
};
#endif // BACKGROUND_H
/usr/share/dbus-1/interfaces/org.foo.Background.xml
<!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="org.foo.Background">
<method name="slot_foo">
</method>
</interface>
</node>
it builds correctly but when run, each of the if
statement fails:
daemon started!
Cannot connect to the D-Bus session bus.
Cannot registerObject 1.
Cannot registerObject 2.
Cannot connect to the D-Bus session bus.
what am I doing wrong?
thanks in advance!