Qt Protobuf
The Qt Protobuf generator plugin generates Qt-based classes from .proto
files. You can use the classes to serialize and deserialize their associated protobuf messages.
Overview
What are protocol buffers?
Protocol buffers are a language-neutral, platform-neutral, and extensible mechanism for serializing structured data, which is compact and easy to use:
- A user defines the structure of data in
.proto
files. - Proto compiler is invoked at build time on
.proto
files to generate code in various programming languages.
The provided format is suitable for network traffic or long-term data storage.
Which problems do protocol buffers solve?
Protocol buffers provide a serialization format for typed and structured data packets up to a few megabytes in size. It allows for the extension with new information without invalidating existing data or requiring code to be updated. For more information, see Protocol Buffers Overview.
Benefits of using Qt Protobuf
You can use a Protocol buffers mechanism in any Qt-based project:
- Without the need to call a protocol buffer compiler explicitly.
- Without manually integrating generated code into the project tree.
- To access the serialization and deserialization API.
- For integrated properties mechanism for generated protobuf messages.
- For integrated gRPC-client support, see Qt GRPC module.
- For integrated QML-types support for generated protobuf messages.
- For using enabled Qt Core and Qt GUI types as a part of *.proto schema.
Using the Module
Google protocol buffers provide a generator called protoc
that supports extensions. The qtprotobufgen Tool is an extension for protoc
and requires that you install protoc
on your host system. For more information about the installation, see Protoc Installation.
- Example of installation for Linux, using apt:
apt install -y protobuf-compiler protoc --version # Ensure compiler version is 3
- Example of installation for Windows using vcpkg
QML API
The QML types of the module are available through the QML and QML_URI options in the qt_add_protobuf macro. The import name is equal to the .proto
package name or defined by QML_URI option in the qt_add_protobuf macro. To use the types, add the following import statement to your .qml file:
import my.proto.package.uri
C API
Using a Qt module's C API requires linking against the module library, either directly or through other dependencies. Several build tools have dedicated support for this, including CMake and qmake.
Building with CMake
Use the find_package()
command to locate the needed module component in the Qt6
package:
find_package(Qt6 REQUIRED COMPONENTS Protobuf)
target_link_libraries(mytarget PRIVATE Qt6::Protobuf)
For more details, see the Build with CMake overview.
Articles and Guides
- CMake Commands in Qt6 Protobuf
- Protobuf Qt Core Types
- Protobuf Qt GUI Types
- Qt Protobuf Well-Known types
References
Examples
Licenses and Attributions
The Qt Protobuf module is available under commercial licenses from The Qt Company. In addition, it is available under free software licenses: The GNU Lesser General Public License, version 3, or the GNU General Public License, version 2, or the GNU General Public License, version 3. See Qt Licensing for further details.
Furthermore, the qtprotobufgen
tool in Qt 6.8.1 may contain third party modules under the following permissive licenses:
BSD 3-clause "New" or "Revised" License |
© 2024 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.