Skip to content

CXX-Qt 0.6 Release

We just released CXX-Qt version 0.6!

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.6, we’re excited to announce that we’re on the road to stabilization! 🥳 We’ve done a (hopefully final) iteration of our API and are now happier than ever to release it.

The new API is now truly in the spirit of CXX. All implementation has moved out of the #[cxx_qt::bridge] and is now simply outside the bridge, like it is for CXX as well. To get an overview of the new API, check out our the planned 1.0 API, which is available starting with this release.

Check out the new release through the usual channels:

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

New CXX-style API

extern “C++Qt”

Like extern "C++" in CXX, the extern "C++Qt" block now only uses a declaration-like syntax to expose functionality to C++/Qt. This is an extension to CXX’s extern "C++" blocks, which allow defining methods as #[qsignal]. These signals can then be emitted from Rust, as well as connected to.

extern “RustQt”

Like extern "Rust" in CXX, the extern "RustQt" block now only uses a declaration-like syntax to expose functionality to C++/Qt. Most existing features have moved to an attribute-syntax, which should look very familiar to existing Qt developers.

The new API includes:

  • #[qobject] is now on a type alias to distinguish between QObject and Rust struct clearer
  • #[qproperty(...)] is now just an attribute on the #[qobject]
  • #[inherit],#[qsignal] & #[qinvokable] have now moved to attributes of external function declarations. They can also now be freely mixed
  • #[cxx_override], #[cxx_final], #[cxx_virtual] are now independent attributes rather than imbedded in #[qinvokable]
Support for Qt enums as shared types

Like shared types in CXX, enums for Qt can be declared using a shared enum in the bridge. These can be defined using the #[qenum] attribute and qnamespace!(...) macro.

Implementation is outside the bridge

Like CXX the implementations on types are now defined outside of the bridge rather than inside. This allows you to choose the prefix for QObjects, as the hard-coded qobject:: prefix is now simply the name of the #[cxx_qt::bridge] module.

Another great new feature in this release: Connecting to Signals via Rust is now possible! Simply use the on_[SIGNAL-NAME]/connect_[SIGNAL-NAME] methods generated for each signal. Closures are supported when connecting to signals as well.

QML modules can now be built

The build system has been improved so that QML modules can now be output by CXX-Qt. This allows for using the declarative syntax of #[qml_element] and #[qml_singleton] on a #[qobject], whereas before Rust QObject types needed to be registered in C++ with the QML engine.

use cxx_qt_build::{CxxQtBuilder, QmlModule};
fn main() {
    CxxQtBuilder::new()
        .qml_module(QmlModule {
            uri: "com.kdab.cxx_qt.demo",
            rust_files: &["src/cxx_qt_bridge.rs"],
            qml_files: &["qml/main.qml"],
            ..Default::default()
        })
        .build();
}

This should also allow for tooling to inspect QML modules that were generated from Rust in the future.

New traits for shaping code generation and interacting with features

Most features of the CXX-Qt code generation are now expressed through Rust traits and can be enabled/disabled or otherwise configured using a syntax similar to CXX shim trait impls.

New features

  • Custom constructors through cxx_qt::Constructor – this drops the requirement for Default on the Rust struct.
  • Easy default-constructor implementation through cxx_qt::Initialize.
  • cxx_qt::CxxQtType trait for reflection and less “magic” methods.

Existing features that can now be enabled/disabled:

  • Threading through the cxx_qt::Threading trait.
  • Locking through the cxx_qt::Locking trait.

Thanks to all of our contributors that helped us with this release

  • Be Wilson
  • Laurent Montel
  • Olivier Le Doeuff
  • Jimmy van Hest
  • Michael (aka @mmMike)
  • Cyril Jacquet
  • Magnus Groß
  • jimmyvanhest

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 *