How to Implement an Async Server with QDBus?
-
I don't see any server in there. You are using the system's server.
As for your question, I think what you are looking for is https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies
@SGaist Thank you very much. I read https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies before asking this question, but that wasn't what I was looking for. The D-Bus adaptors in my project are automatically generated with CMake, and the behavior isn't correct when I directly use the extra QDBusMessage parameter at the end of the search() parameters.
And I figured out how to do it after a few tries. The nitty-gritty of solving the problem is using Q_EMIT to send an async completion signal to the client, with the results passed along at that point.
Here is a simple example to demonstrate the solution:
public slots: // Asynchronously search Q_NOREPLY void async_search(QString keywords) { ... // Pass the results and notify the client once the time-consuming logic is finished Q_EMIT asyncSearchCompleted("test result"); } Q_SIGNALS: void asyncSearchCompleted(const QString& result);
To handle the results on the client side, we connect the signal to a slot:
QDBusInterface *iface = ...; connect(iface, SIGNAL(asyncSearchCompleted(QString)), this, SLOT(handleSearchResult(QString))); public slots: void handleSearchResult(const QString &result) { qDebug() << "Received async search result:" << result; }
-
Hi,
How did you implement your server ?
-
@SGaist Hello,
Here are the header and implementation files for my QDBus server:
https://github.com/dxnu/deepin-anything/blob/master/src/server/include/core/base_event_handler.hhttps://github.com/dxnu/deepin-anything/blob/master/src/server/src/core/base_event_handler.cpp
The base_event_handler class is my QDBus server implementation. I want to make the search() interface asynchronous.
-
I don't see any server in there. You are using the system's server.
As for your question, I think what you are looking for is https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies
-
I don't see any server in there. You are using the system's server.
As for your question, I think what you are looking for is https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies
@SGaist Thank you very much. I read https://doc.qt.io/qt-6/qdbusdeclaringslots.html#delayed-replies before asking this question, but that wasn't what I was looking for. The D-Bus adaptors in my project are automatically generated with CMake, and the behavior isn't correct when I directly use the extra QDBusMessage parameter at the end of the search() parameters.
And I figured out how to do it after a few tries. The nitty-gritty of solving the problem is using Q_EMIT to send an async completion signal to the client, with the results passed along at that point.
Here is a simple example to demonstrate the solution:
public slots: // Asynchronously search Q_NOREPLY void async_search(QString keywords) { ... // Pass the results and notify the client once the time-consuming logic is finished Q_EMIT asyncSearchCompleted("test result"); } Q_SIGNALS: void asyncSearchCompleted(const QString& result);
To handle the results on the client side, we connect the signal to a slot:
QDBusInterface *iface = ...; connect(iface, SIGNAL(asyncSearchCompleted(QString)), this, SLOT(handleSearchResult(QString))); public slots: void handleSearchResult(const QString &result) { qDebug() << "Received async search result:" << result; }
-
S SGaist has marked this topic as solved on