我正在嘗試使用QDBusPendingCallWatcher
來觀看異步呼叫。一些示例代碼是這樣的:如何正確使用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
}
我的問題是:
它看起來像
QDBusPendingCallWatcher
使用shared data pointer inside,是安全的不要手動刪除watcher
指針?只要離開範圍,忘記它?如果我可以讓pendingcall的智能指針去做所有的技巧,我可以在我的班級中只用一個
QDBusPendingCallWatcher
指針來觀察所有的異步調用嗎?像這樣:{ 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*))); }
這會造成災難嗎?或者我應該爲每個呼叫使用多個指針?
謝謝!