Skip to content

CXX-Qt 0.5 Released

We just released CXX-Qt version 0.5!

CXX-Qt is a set of Rust crates for creating bidirectional Rust ⇄ C++ bindings with Qt. It can be used to integrate Rust into C++ applications using CMake or build Rust applications with Cargo. CXX-Qt provides tools for implementing QObject subclasses in Rust that can be used from C++, QML, and JavaScript.

For 0.5, the focus has mainly been on adding more common Qt types, support for Qt container types, support for pointer types, and improving inheritance. Simple QML applications can now be written entirely in Rust, including implementing Qt models, without needing any boilerplate C++ code.

Some of the more extensive developer-facing changes are listed below.

Inheritance

This release introduces new features for interfacing with C++ inheritance from Rust. These allow you to implement subclasses in Rust without needing to write boilerplate C++ code.

When subclassing some Qt classes, you need to call methods of the base class, for example QAbstractItemModel::beginInsertRows, without necessarily overriding those methods in your subclass. A new macro #[cxx_qt::inherit] can be used on extern "C++" blocks to create Rust bindings to those methods inherited from the C++ base class.

There is also an #[inherit] macro that can be used to access signals inherited from the base class. This tells CXX-Qt to skip generating a new Q_SIGNAL and to use the base class version instead.

Types

Bindings for more Qt types have been added to cxx-qt-lib:

  • QByteArray
  • QCoreApplication
  • QGuiApplication
  • QMargins
  • QMarginsF
  • QModelIndex
  • QPersistentModelIndex
  • QQmlApplicationEngine
  • QQmlEngine
  • QTimeZone
  • QStringList
  • QVector2D
  • QVector3D
  • QVector4D

We have also added support for Qt container types:

  • QList<T>
  • QVector<T>
  • QSet<T>
  • QHash<K, V>
  • QMap<K, V>

QList and QVector can easily be converted from Rust slices and into Rust Vecs.

Many Qt types in cxx-qt-lib now have common Rust standard library traits implemented, such as Default, Display, Debug, PartialEq, Eq, PartialOrd, Ord, and arithmetic operators (Add, Sub, Mul, Div traits).

Cargo features have been added to cxx-qt-lib for easily converting between Qt types and types from widely used Rust crates:

  • QColor ⇄ rgb crate
  • QUrl ⇄ http and url crates
  • QByteArray ⇄ bytes crate
  • QDate, QDateTime, and QTime ⇄ chrono and time crates

The code generator now supports pointer types such as *mut T to refer to another Rust QObject in properties, signals, and invokables.

Build System

The build system can now register files from the Qt Resource System at build time, so calling a C++ function to initialize them is no longer needed. Likewise, QML types can now be registered at build time, instead of calling qmlRegisterType from C++ code. Together with new bindings for QGuiApplication and QQmlApplicationEngine, there is no longer a need for boilerplate C++ code to launch a QML application from Rust. The API is still evolving and we hope to improve this area in the next release to support the new ahead-of-time QML compilation tools.

For More Information

Find CXX-Qt on our GitHub repository and view our CHANGELOG for more details about the changes.

We also have a book with a getting started guide.

Discussions and contributions are welcome on GitHub!

About KDAB

If you like this article and want to read similar material, consider subscribing via our RSS feed.

Subscribe to KDAB TV for similar informative short video content.

KDAB provides market leading software consulting and development services and training in Qt, C++ and 3D/OpenGL. Contact us.

FacebookTwitterLinkedInEmail

Categories: KDAB Blogs / KDAB on Qt / Rust / Technical

Tags: / /
Leave a Reply

Your email address will not be published. Required fields are marked *