On this page

QRos2Context Class

Owns the process-wide ROS 2 context and the executor that spins it. More...

Header: #include <QtRos2Core/qros2context.h>
CMake: find_package(Qt6 REQUIRED COMPONENTS QtRos2)
target_link_libraries(mytarget PRIVATE Qt6::QtRos2)
Status: Experimental Extension

Public Functions

const std::shared_ptr<rclcpp::Executor> &executor() const

Static Public Members

void init()
void init(int argc, char **argv, bool useMultithreadedExecutor = false, size_t threadCount = 0)
QRos2Context &instance()
bool isInitialized()

Detailed Description

Every ROS 2 entity in a process shares one context: rclcpp has to be initialized before any node is created, and something has to spin an executor for callbacks to arrive. QRos2Context is that singleton.

QML applications do not normally touch it, because declaring a Node initializes the context on demand. A C++ application that creates entities before any QML is loaded, or that wants to pass ROS command-line arguments through, calls init() from main() first:

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QRos2Context::init(argc, argv);
    ...
}

The context shuts down with the application.

Member Function Documentation

const std::shared_ptr<rclcpp::Executor> &QRos2Context::executor() const

Returns the executor spinning this context, for code that needs to add or remove nodes directly.

[static] void QRos2Context::init()

Initializes the ROS 2 context with no command-line arguments and a single-threaded executor.

Does nothing if the context is already initialized, so it is safe to call from several places.

[static] void QRos2Context::init(int argc, char **argv, bool useMultithreadedExecutor = false, size_t threadCount = 0)

Initializes the ROS 2 context, passing argc and argv on to rclcpp so that ROS command-line arguments such as --ros-args are honoured.

Pass true for useMultithreadedExecutor to spin callbacks on several threads, with threadCount threads; 0 lets rclcpp choose. Callbacks then run on executor threads rather than the Qt main thread, so anything they touch must be thread-safe.

Does nothing if the context is already initialized.

[static] QRos2Context &QRos2Context::instance()

Returns the singleton instance, initializing the context if it is not already initialized.

[static] bool QRos2Context::isInitialized()

Returns true if the ROS 2 context has been initialized.

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