2

QDBusPendingCallWatcher非同期呼び出しを監視するために使用しようとしています。このようないくつかのサンプル コード:

{
    // interface = new QDBusInterface(...);
    QDBusPendingCall pcall = interface->asyncCall("query");
    QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pcall, this);
    QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(handler(QDBusPendingCallWatcher*)));
}

およびハンドラー関数:

void Client::handler(QDBusPendingCallWatcher* call)
{       
    QDBusPendingReply<QString> reply = *call; 
    // do something
}

私の質問は次のとおりです。

  1. 内部で共有データ ポインターをQDBusPendingCallWatcher使用しているように見えますが、ポインターを手動で削除しない方が安全ですか? スコープを離れて忘れますか?watcher

  2. pendingcall のスマート ポインターにすべてのトリックを実行させることができる場合QDBusPendingCallWatcher、クラス内のポインターを 1 つだけ使用して、すべての非同期呼び出しを監視できますか? このような:

    {
        QDBusPendingCall pcall = interface->asyncCall("query");
        watcher = new QDBusPendingCallWatcher(pcall, this);
        QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(handleOne(QDBusPendingCallWatcher*)));
    
        pcall = interface->asyncCall("anotherQuery");
        watcher = new QDBusPendingCallWatcher(pcall, this);
        QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(handleTwo(QDBusPendingCallWatcher*)));
    }
    

    これは災害になりますか?または、呼び出しごとに複数のポインターを使用する必要がありますか?

ありがとう!

4

1 に答える 1

1

QDBusPendingCallWatcherのドキュメントを詳しく見てください。

上記のコードで接続されているスロットは、次のようになります。

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

QObject :: deleteLaterの呼び出しが重要です。これは、実行がイベントループに戻るとすぐに、Qtがオブジェクトを削除することを意味します。

deleteLater内部に電話をかける限り、どこにでもClient::handler(...)電話をかける必要はありません。delete watcher;あなたが確認しなければならない唯一のことはcall、スロットが戻った後、誰も後ろのオブジェクトを使用しないことです。

于 2013-02-06T12:38:44.203 に答える