On this page

Simple Service Client

Calling a std_srvs/SetBool service declaratively from QML by binding the request property.

Simple Service Client is the calling side of the Simple Service example: it wraps one SetBoolServiceClient in a Node and keeps a std_srvs/SetBool service in sync with a switch — the switch expresses the desired lamp state, and the service response reports what the server actually did.

The Simple Service Client window: an editable topic, the lamp switch, and the last response.

Running the example

  1. Source your ROS 2 environment, then launch Qt Creator from the same shell so it can find the generated QML modules.
  2. Open examples/simple_service_client/CMakeLists.txt in Qt Creator, then build and run.
  3. Provide the service by running the Simple Service example with the same topic.

Calling a service by binding

A service client is declared as a child of a Node. Binding the request property is all it takes to call the service: every change of the bound value schedules a call (multiple changes per event-loop iteration coalesce into one), and the result lands in the read-only response property:

import QtRos2.StdSrvs

Node {
    id: rosNode
    nodeName: "simple_service_client_node"

    SetBoolServiceClient {
        id: lampClient
        topic: topicField.text
        request: lampSwitch.checked
    }
}
Label {
    text: lampClient.response.message
}

The request binding behaves like desired state that is reconciled with the server: if the service is not available yet, or a call is still in flight, the latest value is remembered and dispatched as soon as possible. Flip the switch before starting the server and the state is applied the moment the server appears. This behavior is controlled by autoCall (on by default; auto-calls only happen when request is actually written).

Run it against Simple Service and burn the bulb out: the response switches to success: false with an explanatory message — service failure is in-band, part of the response value, so it is bindable state like everything else.

Imperative calls

callService() remains for one-shot, imperative calls; it returns a JavaScript promise:

lampClient.callService(true).then(response => {
    console.log(response.success, response.message)
}, error => {
    console.log("call failed:", error)
})

Either way, isServiceReady reports whether a server was discovered on the graph, isCallPending is true while a response is awaited, and transport-level failures (the server disappearing mid-call) are reported through the callFailed signal and the promise rejection.

Files:

Images:

See also Simple Service and Qt ROS2 std_srvs QML Types.

© 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.