Services

Currently, there is no service client or service server implementation (It’s on my todo-list, though). However, there is a Service singleton that can be used to call a service.

Here’s a short modified example of the service example provided in the Examples.

Button {
  onClicked: {
    var result = Service.call("/add_two_ints", "roscpp_tutorials/TwoInts",
                              { a: inputA.value, b: inputB.value })
    textResult.text = !!result ? ("Result: " + result.sum) : "Failed"
  }
}

The first argument is the service that is called, the second is the type of the service, and the final argument is the request that is sent.

The service either returns false if the call failed, true if the call was successful but the service description has an empty return message, and the return message of the service otherwise.

A service call is blocking and it is usually not a good idea to make blocking work on the UI thread. For that reason, there is also the callAsync method which runs the service call on a new separate thread and is additionally passed an optional callback which is called after the service call completed.

Button {
  onClicked: {
    textResult.text = "Loading..."
    Service.callAsync(
      "/add_two_ints", "roscpp_tutorials/TwoInts",
      { a: inputA.value, b: inputB.value },
      function (result) {
        textResult.text = !!result ? ("Result: " + result.sum) : "Failed"
      })
  }
}

API

class qml_ros_plugin::Service : public QObject

Public Functions

QVariant call(const QString &service, const QString &type, const QVariantMap &req)

Calls a service and returns the result.

Parameters
  • service – The service topic.

  • type – The type of the service, e.g., “roscpp_tutorials/TwoInts”

  • req – The service request, i.e., a filled request message of the service type, e.g., “roscpp_tutorials/TwoIntsRequest”

Returns

False, if request was not successful, true if the response message is empty and the translated service response, e.g., “roscpp_tutorials/TwoIntsResponse”, otherwise.

void callAsync(const QString &service, const QString &type, const QVariantMap &req, const QJSValue &callback = QJSValue())

Calls a service asynchronously returning immediately. Once the service call finishes, the optional callback is called with the result if provided.

Parameters
  • service – The service topic.

  • type – The type of the service, e.g., “roscpp_tutorials/TwoInts”

  • req – The service request, i.e., a filled request message of the service type, e.g., “roscpp_tutorials/TwoIntsRequest”

  • callback – The callback that is called once the service has finished.