The Qt OPC UA data type generator

Starting with Qt 6.7, Qt OPC UA comes with a data type generator named qopcuaxmldatatypes2cpp which generates Qt OPC UA compatible C++ enumerations and data classes from enums and structured types in .bsd files. As a secondary function, node id enums are generated from CSV files.

The generator is a command line tool which can be controlled by the following arguments:

LongShortPurpose
--input-iA .bsd file for which all contained enum and struct types will be generated. Can be used more than once to generate code for multiple models.
--dependencyinput-dA dependency input .bsd file where only types required by a struct from an input file passed via --input will be generated. Can be used more than once to accommodate models with dependencies to multiple other models.
--nodeids-nA name and the path to a .csv file with node ids to generate an enum for, e.g. MyModel:/path/to/nodeids.csv => enum class MyModelNodeId. Can be specified multiple times.
--output-oThe output directory where the generated files will be placed.
--prefix-pThe prefix for the generated file, enum and class names. The default value is GeneratedOpcUa.
--bundle-bGenerate bundle files <prefix>datatypes.h and <prefix>datatypes.cpp which #include all generated files

The following files are generated for each run:

  • One .h file containing a namespace with all enumerated types
  • One .h and one .cpp file for each structured type
  • One .h and one .cpp file containing encoding and decoding methods
  • One .h file containing the node id enums if -n is given at least once
  • The bundle files <prefix>datatypes.h and <prefix>datatypes.cpp if -b is given

CMake Integration

The qt_opcua_generate_datatypes() function takes one or multiple input .bsd files and any required dependency .bsd files and invokes qopcuaxmldatatypes2cpp. The generated source and header files are written to OUTPUT_DIR and are added to the target specified as the first argument.

The OUTPUT_DIR or its parent directory must also be added to the target's include directories.

The following CMakeLists.txt snippet shows how to integrate the code generator in to a CMake based project:

 find_package(Qt6 REQUIRED COMPONENTS Core OpcUa)

 qt_standard_project_setup()

 qt_add_executable(my_codegen
     mycodegen.cpp
 )

 qt_opcua_generate_datatypes(
     my_codegen # The target to add the generated files to
     INPUT_BSD "${CMAKE_CURRENT_SOURCE_DIR}/mymodel.bsd" # Must be set at least once if INPUT_CSV_MAP is not set
     INPUT_BSD "${CMAKE_CURRENT_SOURCE_DIR}/myothermodel.bsd"
     DEPENDENCY_BSD "${CMAKE_CURRENT_SOURCE_DIR}/mydependency.bsd" # Optional
     INPUT_CSV_MAP "MyModel:${CMAKE_CURRENT_SOURCE_DIR}/mymodel.csv" # Must be set at least once if INPUT_BSD is not set
     INPUT_CSV_MAP "MyOtherModel:${CMAKE_CURRENT_SOURCE_DIR}/myothermodel.csv"
     PREFIX "GeneratedOpcUa" # Mandatory
     OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated" # Mandatory
 )

 target_include_directories(my_codegen
     PRIVATE
         ${CMAKE_CURRENT_BINARY_DIR}
 )

 target_link_libraries(my_codegen PRIVATE Qt6::Core Qt6::OpcUa)

The generated files can then be used in any header or source file by including the two top-level headers for the data types and the encoder/decoder class.

 #include <generated/generatedopcuabinarydeencoder.h> // If INPUT_BSD was set
 #include <generated/generatedopcuadatatypes.h> // If INPUT_BSD was set
 #include <generated/generatedopcuanodeids.h> // If INPUT_CSV_MAP was set