QDBusContext Class
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 |
详细说明
当对象中的槽因信号传递或远程方法调用而被调用时,有时需要了解发生该事件的上下文。特别是,如果槽决定在稍后时机发送响应,或者打算返回错误,则需要该上下文。
QDBusContext 类是一种访问上下文的替代方案,它无需修改由Qt D-Bus XML 编译器(qdbusxml2cpp)生成的代码。
QDBusContext 的使用方法是,通过从使用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, &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()。
© 2026 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.