C

Enabling Qt Safe Renderer in CMake Project

The following topics describe how you enable Qt Safe Renderer in your CMake project.

Note: The Qt Safe Renderer Indicators example and its CMakeLists file provide a simple example of most of the variables. You can use Indicators also in the host environment. For more information, see Indicators: Creating Safety-Critical UI.

Using Qt Safe Renderer Project Template in Qt Creator

From Qt Creator 10.0.1 onwards, you can generate a new Qt Safe Renderer project by using a project template. The Qt Safe Renderer Application 2.1 project template supports both cmake and qmake build systems.

First, you need to enable the Qt Safe Renderer plugin in Qt Creator as follows:

  1. Select Help > About Plugins.
  2. In Device Support, check SafeRenderer.

Generate the new Qt Safe Renderer project as follows:

  1. Select File > New Project > Application (Qt Safe Renderer).
  2. Select Choose.
  3. Select Qt Safe Renderer Application 2.1.

  4. Follow instructions on the wizard to create a new project. In Define build system, select cmake.

The generated project provides a base for your own Qt Safe Renderer project.

Qt Safe Renderer in CMakeLists.txt File

The following topics describe how you can enable Qt Safe Renderer in a CMake project via the CMakeLists file. For a complete example, generate a Qt Safe Renderer CMake project as instructed in Using Qt Safe Renderer Project Template in Qt Creator.

Building for Target Device and Host

By default, building for host is disabled and building for the target device is enabled:

# Define the condition for HOST_BUILD
set(HOST_BUILD OFF)  # Default to OFF

You should use HOST_BUILD only for demonstration purposes on host, not for production purposes of any kind.

The host build is automatically enabled when you are not cross-compiling and your host is either Linux, Windows, or macOS:

if (NOT CMAKE_CROSSCOMPILING AND (NOT UNIX OR NOT (CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "arm|aarch64") OR APPLE))
    set(HOST_BUILD ON)
endif()

Finding Qt Safe Renderer Components

List the required components via find_package. For Qt Safe Renderer, you need SafeRenderer, SafeRendererTools, and SafePlatformAdaptation as follows:

find_package(Qt6 REQUIRED COMPONENTS Core Qml Quick SafeRenderer SafeRendererTools SafePlatformAdaptation)

Defining Safe Resources

safeqmls lists the QML files containing safety-critical UI elements as follows:

set (safeqmls
"main.qml"
)

resource_files lists the resource files that are passed to Qt Safe Layout Tool:

set(resource_files
    "qml.qrc"
)

Add the safe QML files and safe resources to Qt Safe Renderer as follows. If you build for host, you need to compile binary resources into source code with qt6_add_resources:

qsr_add_safelayout(generatelayout_myproject SAFE_QMLS ${safeqmls}
                                  OUTPUT_PATH ${CMAKE_CURRENT_LIST_DIR}/layoutData
                                  SAFE_RESOURCE "${CMAKE_CURRENT_LIST_DIR}/safeasset.qrc"
                                  INPUT_RESOURCES resource.bin)
qsr_add_resource(buildresource_myproject sources "${CMAKE_CURRENT_LIST_DIR}/safeasset.qrc")

if (HOST_BUILD)
    qt6_add_resources(sources ${resource_files})
endif()

For more information, see QSR_ADD_SAFE_LAYOUT, QSR_ADD_RESOURCE, and qt_add_resources.

When you build for the target device, define the Qt Safe Renderer target link libraries Qt::SafeRenderer and Qt::SafePlatformAdaptation as follows:

target_link_libraries(myproject PUBLIC
    Qt::SafeRenderer
    Qt::SafePlatformAdaptation
)

When you build to host, you need to link against the Qt libraries as follows:

if (HOST_BUILD)
target_compile_definitions(myproject PUBLIC
    HOST_BUILD
)

target_link_libraries(myproject PUBLIC
    Qt::Quick
    Qt::Widgets
    Qt::Qml
)

Enabling Localization

Enable localization by adding safe languages and translations into the safe layout:

qsr_add_safelayout(generatelayout_localization SAFE_QMLS ${safeqmls}
                                  SAFE_LANGUAGES en fi de uk el ar da et no sv
                                  SAFE_TRANSLATION "${CMAKE_CURRENT_LIST_DIR}/translationFiles/safeui"
                                  OUTPUT_PATH "${CMAKE_CURRENT_LIST_DIR}/layoutData"
                                  SAFE_LAYOUT_FONTS "${CMAKE_CURRENT_LIST_DIR}/fonts"
                                  SAFE_RESOURCE "${CMAKE_CURRENT_LIST_DIR}/safeasset.qrc"
                                  INPUT_RESOURCES resource.bin)

For more information, see QSR_ADD_SAFE_LAYOUT.

Enabling Output Verification

Enable Output Verification as follows:

#Enable when using monitor feature:
target_compile_definitions(myproject PRIVATE USE_OUTPUTVERIFIER)

In the Monitor code, define the monitor configuration as follows:

qsr_monitorconfig(generatedata_monitor sources XML LAYOUT_FILES ${output_srl_files} OUTPUTDIR "${CMAKE_CURRENT_LIST_DIR}/data")

For more information, see QSR_MONITORCONFIG.

Monitor: Verifying the Rendering Output provides an example of the monitor.

Building Qt Safe Renderer CMake Project

Build the CMake project in Qt Creator as follows:

  1. Select Build > Run CMake. Running the cmake tool generates a folder that is defined in the OUTPUT_PATH variable.
  2. Select Build > Build Project <a project name>. Building the project generates the .srb, .srt, .srl, and .qpf2 files under the folder that the OUTPUT_PATH variable defines. For more information about these files, see Generating Safe Layout Data.

Available under certain Qt licenses.
Find out more.