6.0.0-rc4Source

QDBusContext Class

The QDBusContext class allows slots to determine the D-Bus context of the calls. More…

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

List of all members, including inherited members

Public Functions

QDBusContext()
virtual ~QDBusContext()
boolcalledFromDBus() const
QDBusConnectionconnection() const
const QDBusMessage &message() const
boolisDelayedReply() const
voidsetDelayedReply(bool enable) const
voidsendErrorReply(const QString &name, const QString &message = QString()) const
voidsendErrorReply(QDBusError::ErrorType type, const QString &message = QString()) const

Detailed Description

Inherit QDBusContext in an exported object or adaptor, next to QObject. While a slot runs because of a D-Bus call, message() is the call and connection() the connection it came on. A slot can then reply later, or with an error:

class Store : public QObject, protected QDBusContext {
    Q_OBJECT
public slots:
    QString Get(const QString &key) {
        if (!m_values.contains(key))
            sendErrorReply(QDBusError::InvalidArgs, "no such key");
        return m_values.value(key);
    }
};

See also QDBusConnection::registerObject().

Differences from Qt

The destructor is virtual, and QDBusContext cannot be copied. The context is also set on the object that owns an adaptor while the adaptor's slot runs.

Member Function Documentation

QDBusContext::QDBusContext()

Constructs a context, which is empty until a D-Bus call arrives.

[virtual] QDBusContext::~QDBusContext()

Destroys the context.

bool QDBusContext::calledFromDBus() const

Returns true if the slot that is running was called over D-Bus.

QDBusConnection QDBusContext::connection() const

Returns the connection on which the current call arrived. Valid only while calledFromDBus() is true.

bool QDBusContext::isDelayedReply() const

Returns true if the reply to the current call will be sent later.

const QDBusMessage & QDBusContext::message() const

Returns the message of the current call. Valid only while calledFromDBus() is true.

void QDBusContext::sendErrorReply(const QString &name, const QString &message = QString()) const

Sends the error name with the text message as the reply to the current call. The slot's return value is ignored.

void QDBusContext::sendErrorReply(QDBusError::ErrorType type, const QString &message = QString()) const

This is an overloaded function.

Sends an error of the type type with the text message as the reply.

void QDBusContext::setDelayedReply(bool enable) const

Sets whether the reply to the current call is sent later to enable. The slot's return value is then ignored; keep message() and send the reply with connection().send(message().createReply(...)).