QDBusPendingCallWatcher Class
QDBusPendingCallWatcher 类提供了一种等待异步回复的便捷方法。更多
Header: | #include <QDBusPendingCallWatcher> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS DBus) target_link_libraries(mytarget PRIVATE Qt6::DBus) |
qmake: | QT += dbus |
继承: | QObject 和QDBusPendingCall |
公共函数
QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent = nullptr) | |
virtual | ~QDBusPendingCallWatcher() |
bool | isFinished() const |
void | waitForFinished() |
信号
void | finished(QDBusPendingCallWatcher *self = nullptr) |
详细说明
QDBusPendingCallWatcher 提供了finished() 信号,当有回复到达时,该信号就会发出。
它通常像下面的示例一样使用:
QDBusPendingCall async = iface->asyncCall("RemoteMethod", value1, value2); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this); QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &DBus_PendingCall_Interface::callFinishedSlot);
请注意,由于 QDBusPendingCallWatcher 也继承自QDBusPendingCall 对象,因此无需保留该对象。
上述代码所连接的槽可能类似于下面的内容:
void DBus_PendingCall_Interface::callFinishedSlot(QDBusPendingCallWatcher *call) { QDBusPendingReply<QString, QByteArray> reply = *call; if (reply.isError()) { showError(); } else { QString text = reply.argumentAt<0>(); QByteArray data = reply.argumentAt<1>(); showReply(text, data); } call->deleteLater(); }
注意使用QDBusPendingReply 验证回复中的参数类型。如果回复不完全包含两个参数(一个字符串和一个QByteArray ),QDBusPendingReply::isError() 将返回 true。
另请参阅 QDBusPendingReply 。
成员函数文档
[explicit]
QDBusPendingCallWatcher::QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent = nullptr)
创建一个 QDBusPendingCallWatcher 对象,用于监视异步待处理调用call 的回复,并将该对象的父对象设置为parent 。
[virtual noexcept]
QDBusPendingCallWatcher::~QDBusPendingCallWatcher()
销毁此对象。如果该QDBusPendingCallWatcher 对象是未完成的待处理调用的最后一个引用,则该调用将被取消。
[signal]
void QDBusPendingCallWatcher::finished(QDBusPendingCallWatcher *self = nullptr)
当待处理调用结束且回复可用时,就会发出该信号。self 参数是指向对象本身的指针,传递该指针是为了方便槽访问属性并确定回复的内容。
bool QDBusPendingCallWatcher::isFinished() const
如果待处理调用已处理完毕且已收到回复,则返回true
。
请注意,只有在调用waitForFinished() 或发生外部 D-Bus 事件(通常只有在返回事件循环执行时才会发生)时,该函数才会改变状态。
另请参见 QDBusPendingReply::isFinished()。
void QDBusPendingCallWatcher::waitForFinished()
暂停调用线程的执行,直到收到并处理回复。此函数返回后,isFinished() 应返回 true,表明回复的内容已准备就绪,可以开始处理。
© 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.