In questa pagina

QDBusPendingCallWatcher Class

La classe QDBusPendingCallWatcher offre un modo pratico per attendere le risposte asincrone. Per saperne di più...

Intestazione: #include <QDBusPendingCallWatcher>
CMake: find_package(Qt6 REQUIRED COMPONENTS DBus)
target_link_libraries(mytarget PRIVATE Qt6::DBus)
qmake: QT += dbus
Eredita da:QObject e QDBusPendingCall

Funzioni pubbliche

QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent = nullptr)
virtual ~QDBusPendingCallWatcher()
bool isFinished() const
void waitForFinished()

Segnali

void finished(QDBusPendingCallWatcher *self = nullptr)

Descrizione dettagliata

QDBusPendingCallWatcher fornisce il segnale finished() che verrà emesso all'arrivo di una risposta.

Di solito viene utilizzato come nell'esempio seguente:

    QDBusPendingCall async = iface->asyncCall("RemoteMethod", value1, value2);
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(async, this);

    QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this,
                     &DBus_PendingCall_Interface::callFinishedSlot);

Si noti che non è necessario mantenere l'oggetto QDBusPendingCall originale, poiché anche QDBusPendingCallWatcher eredita da quella classe.

Lo slot a cui è collegato il codice sopra riportato potrebbe essere simile al seguente:

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();
}

Si noti l'uso di ` QDBusPendingReply ` per convalidare i tipi degli argomenti nella risposta. Se la risposta non contenesse esattamente due argomenti (una stringa e un ` QByteArray`), ` QDBusPendingReply::isError()` restituirà `true`.

Vedi anche QDBusPendingReply.

Documentazione sulle funzioni membro

[explicit] QDBusPendingCallWatcher::QDBusPendingCallWatcher(const QDBusPendingCall &call, QObject *parent = nullptr)

Crea un oggetto `QDBusPendingCallWatcher` per monitorare le risposte relative alla chiamata asincrona in sospeso ` call ` e imposta come genitore di questo oggetto ` parent`.

[virtual noexcept] QDBusPendingCallWatcher::~QDBusPendingCallWatcher()

Elimina questo oggetto. Se questo oggetto ` QDBusPendingCallWatcher ` era l'ultimo riferimento alla chiamata in sospeso non completata, la chiamata verrà annullata.

[signal] void QDBusPendingCallWatcher::finished(QDBusPendingCallWatcher *self = nullptr)

Questo segnale viene emesso quando la chiamata in sospeso è terminata e la relativa risposta è disponibile. Il parametro ` self ` è un puntatore all'oggetto stesso, passato per comodità in modo che lo slot possa accedere alle proprietà e determinare il contenuto della risposta.

bool QDBusPendingCallWatcher::isFinished() const

Restituisce ` true ` se l'elaborazione della chiamata in sospeso è terminata e la risposta è stata ricevuta.

Si noti che questa funzione cambia stato solo se si chiama waitForFinished() o se si verifica un evento D-Bus esterno, cosa che in genere avviene solo quando si ritorna all’esecuzione del ciclo di eventi.

Vedi anche QDBusPendingReply::isFinished().

void QDBusPendingCallWatcher::waitForFinished()

Sospende l'esecuzione del thread chiamante fino a quando non viene ricevuta ed elaborata la risposta. Una volta che questa funzione ha restituito il proprio valore, la funzione ` isFinished()` dovrebbe restituire `true`, indicando che il contenuto della risposta è pronto per essere elaborato.

Vedi anche ` 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.