本页内容

QDBusPendingCallWatcher Class

QDBusPendingCallWatcher 类提供了一种等待异步响应的便捷方式。更多内容...

头文件: #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);

请注意,无需保留原始的QDBusPendingCall 对象,因为QDBusPendingCallWatcher也继承自该类。

上述代码连接的插槽可以类似于以下内容:

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 事件时才会更改状态,而外部 D-Bus 事件通常仅在返回事件循环执行时才会发生。

另请参阅 QDBusPendingReply::isFinished()。

void QDBusPendingCallWatcher::waitForFinished()

暂停调用线程的执行,直到收到并处理完响应为止。该函数返回后,isFinished() 应返回 true,表示响应内容已准备好进行处理。

另请参阅 QDBusPendingReply::waitForFinished()。

© 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.