QDBusContext 类

QDBusContext 类允许槽确定调用的 D-Bus 上下文。 更多...

头: #include <QDBusContext>
CMake: find_package(Qt6 REQUIRED COMPONENTS DBus)
target_link_libraries(mytarget PRIVATE Qt6::DBus)
qmake: QT += dbus

公共函数

QDBusContext ()
~QDBusContext ()
bool calledFromDBus () const
QDBusConnection connection () const
bool isDelayedReply () const
const QDBusMessage & message () const
void sendErrorReply (const QString & name , const QString & msg = QString()) const
void sendErrorReply (QDBusError::ErrorType type , const QString & msg = QString()) const
void setDelayedReply (bool enable ) const

详细描述

When a slot is called in an object due to a signal delivery or due to a remote method call, it is sometimes necessary to know the context in which that happened. In particular, if the slot determines that it wants to send the reply at a later opportunity or if it wants to reply with an error, the context is needed.

The QDBusContext class is an alternative to accessing the context that doesn't involve modifying the code generated by the Qt D-Bus XML 编译器 (qdbusxml2cpp) .

QDBusContext is used by subclassing it from the objects being exported using QDBusConnection::registerObject ()。以下范例阐明用法:

class MyObject: public QObject,
                protected QDBusContext
{
    Q_OBJECT
    QDBusConnection conn;
    QDBusMessage msg;
    //...
protected slots:
    void process();
public slots:
    void methodWithError();
    QString methodWithDelayedReply();
};
void MyObject::methodWithError()
{
    sendErrorReply(QDBusError::NotSupported,
                   "The method call 'methodWithError()' is not supported");
}
QString MyObject::methodWithDelayedReply()
{
    conn = connection();
    msg = message();
    setDelayedReply(true);
    QMetaObject::invokeMethod(this, "process", Qt::QueuedConnection);
    return QString();
}
					

The example illustrates the two typical uses, that of sending error replies and that of delayed replies.

Note: do not subclass QDBusContext and QDBusAbstractAdaptor at the same time. QDBusContext should appear in the real object, not the adaptor. If it's necessary from the adaptor code to determine the context, use a public inheritance and access the functions via QObject::parent ().

成员函数文档编制

QDBusContext:: QDBusContext ()

Constructs an empty QDBusContext.

QDBusContext:: ~QDBusContext ()

空的析构函数。

bool QDBusContext:: calledFromDBus () const

返回 true if we are processing a D-Bus call. If this function returns true , the rest of the functions in this class are available.

Accessing those functions when this function returns false is undefined and may lead to crashes.

QDBusConnection QDBusContext:: connection () const

Returns the connection from which this call was received.

bool QDBusContext:: isDelayedReply () const

返回 true 若此调用会拥有延迟回复。

另请参阅 setDelayedReply ().

const QDBusMessage &QDBusContext:: message () const

Returns the message that generated this call.

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

发送错误 name as a reply to the caller. The optional msg parameter is a human-readable text explaining the failure.

If an error is sent, the return value and any output parameters from the called slot will be ignored by Qt D-Bus.

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

这是重载函数。

发送错误 type as a reply to the caller. The optional msg parameter is a human-readable text explaining the failure.

If an error is sent, the return value and any output parameters from the called slot will be ignored by Qt D-Bus.

void QDBusContext:: setDelayedReply ( bool enable ) const

Sets whether this call will have a delayed reply or not.

enable is false, Qt D-Bus will automatically generate a reply back to the caller, if needed, as soon as the called slot returns.

enable is true, Qt D-Bus will not generate automatic replies. It will also ignore the return value from the slot and any output parameters. Instead, the called object is responsible for storing the incoming message and send a reply or error at a later time.

Failing to send a reply will result in an automatic timeout error being generated by D-Bus.

另请参阅 isDelayedReply ().