QDBusContext Class

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

Header: #include <QDBusContext>
CMake.QDBusContext 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

详细说明

当对象中的槽因信号传递或远程方法调用而被调用时,有时需要知道发生调用的上下文。特别是,如果槽确定要在稍后的机会发送回复,或者要以错误的方式回复,就需要知道上下文。

QDBusContext 类是访问上下文的另一种方法,它不需要修改Qt D-Bus XML 编译器(qdbusxml2cpp)生成的代码。

通过使用QDBusConnection::registerObject() 从导出对象中对 QDBusContext 进行子类化,即可使用 QDBusContext。下面的示例说明了其用法:

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, &MyObject::process, Qt::QueuedConnection);
    return QString();
}

该示例说明了两种典型用法:发送错误回复和延迟回复。

注意:不要同时子类化 QDBusContext 和QDBusAbstractAdaptor 。QDBusContext 应出现在真实对象中,而不是适配器中。如果需要在适配器代码中确定上下文,请使用公共继承,并通过QObject::parent() 访问函数。

成员函数文档

QDBusContext::QDBusContext()

构造一个空的 QDBusContext。

[noexcept] QDBusContext::~QDBusContext()

空析构函数

bool QDBusContext::calledFromDBus() const

如果正在处理 D-Bus 调用,则返回true 。如果该函数返回true ,则该类中的其他函数可用。

在此函数返回false 时访问这些函数是未定义的,可能会导致程序崩溃。

QDBusConnection QDBusContext::connection() const

返回接收该呼叫的连接。

bool QDBusContext::isDelayedReply() const

如果该调用将有延迟回复,则返回true

另请参阅 setDelayedReply().

const QDBusMessage &QDBusContext::message() const

返回生成此调用的信息。

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

向调用者发送错误name 作为回复。可选的msg 参数是一段人类可读的文本,用于解释故障。

如果发送了错误信息,Qt D-Bus 将忽略返回值和任何来自被调用槽的输出参数。

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

这是一个重载函数。

将错误type 作为回复发送给调用者。可选的msg 参数是一段可读文本,用于解释故障。

如果发送了错误信息,Qt D-Bus 将忽略返回值和任何来自被调用槽的输出参数。

void QDBusContext::setDelayedReply(bool enable) const

设置此调用是否有延迟回复。

如果enable 为 false,则Qt D-Bus 会在被调用槽返回后,根据需要自动生成回复给调用者。

如果enable 为 true,则Qt D-Bus 不会自动生成回复。它还将忽略槽的返回值和任何输出参数。取而代之的是,被调用对象负责存储收到的信息,并在稍后时间发送回复或错误信息。

如果未发送回复,D-Bus 将自动生成超时错误。

另请参见 isDelayedReply().

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.