6.0.0-rc4Source

QDBusAbstractAdaptor Class

The QDBusAbstractAdaptor class is the base class of D-Bus adaptor classes. More…

Header#include <QDBusAbstractAdaptor>
CMaketarget_link_libraries(app PRIVATE Nt::DBus)
ModuleNt D-Bus C++ Classes
InheritsQObject

List of all members, including inherited members

Public Functions

Protected Functions

QDBusAbstractAdaptor(QObject *parent)
voidsetAutoRelaySignals(bool enable)
boolautoRelaySignals() const

Detailed Description

An adaptor exports one D-Bus interface of the object that is its parent. Its public slots become methods, its signals D-Bus signals and its properties D-Bus properties; the class information "D-Bus Interface" names the interface:

class Clock : public QObject {
    Q_OBJECT
public:
    QString now() const { return QDateTime::currentDateTime().toString(Qt::ISODate); }
signals:
    void alarm(const QString &message);
};

class ClockAdaptor : public QDBusAbstractAdaptor {
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "org.example.Clock")
    Q_PROPERTY(int volume READ volume WRITE setVolume)
public:
    explicit ClockAdaptor(Clock *clock) : QDBusAbstractAdaptor(clock), m_clock(clock) {
        setAutoRelaySignals(true); // Clock::alarm() is emitted as ClockAdaptor::alarm()
    }
    int volume() const { return m_volume; }
    void setVolume(int volume) { m_volume = volume; }

public slots:
    QString Now() { return m_clock->now(); }
    void SetAlarm(int seconds, const QString &message) {
        QTimer::singleShot(seconds * 1000, m_clock, [this, message] { emit m_clock->alarm(message); });
    }

signals:
    void alarm(const QString &message);

private:
    Clock *m_clock;
    int m_volume = 5;
};

Create the adaptor with the object as its parent, then register the object with QDBusConnection::registerObject(). The adaptor is destroyed with the object.

See also QDBusConnection::registerObject() and QDBusContext.

Differences from Qt

The adaptor must exist when the object is registered for its signals to be relayed. The class information "D-Bus Introspection", if present, is used as the introspection data of the interface as it is.

Member Function Documentation

[explicit, protected] QDBusAbstractAdaptor::QDBusAbstractAdaptor(QObject *parent)

Constructs an adaptor for parent, which becomes its parent.

[override] QDBusAbstractAdaptor::~QDBusAbstractAdaptor()

Destroys the adaptor.

[protected] bool QDBusAbstractAdaptor::autoRelaySignals() const

Returns true if the adaptor relays the signals of its parent.

[protected] void QDBusAbstractAdaptor::setAutoRelaySignals(bool enable)

Sets whether each signal of the adaptor is emitted when the parent emits a signal of the same name and parameters to enable. Call it in the constructor of the subclass.