Skip to content

CXX-Qt 0.4 Released

We just released CXX-Qt version 0.4!

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.4, most of the internals of CXX-Qt have been refactored into stages, which means it is easier to maintain. There are various small changes to the API, such as the capability of using attributes in more places rather than magic struct names. Relocatable Qt types have been marked as trivial to CXX (which means they don’t need to be wrapped in pointers).

Some of the larger developer-facing changes are listed below.

Refactor of API So That CXX-Qt is a Superset of CXX

You can now combine CXX definitions into the CXX-Qt bridge macro.

This allows you to define custom types with CXX extern blocks that can then be used for CXX-Qt purposes, such as properties, invokables, and signals.

For example, in the bridge below, a CXX extern “C++” block is used to define QString as a type. Then, this can be used as a property in the QObject definition.

#[cxx_qt::bridge]
mod my_object {
    unsafe extern "C++" {
        include!("cxx-qt-lib/qstring.h");
        type QString = cxx_qt_lib::QString;
    }

    #[cxx_qt::qobject]
    #[derive(Default)]
    pub struct MyObject {
        #[qproperty]
        number: i32,
        #[qproperty]
        string: QString,
    }
}

Rewrite of the Build System

Custom CMake files have been removed from the build process; Corrosion is used instead, for CMake builds. This allows for a cleaner build that configures and builds Rust code at the correct times.

There is also support for Cargo-only builds for Rust developers who don’t use CMake.

Simplified Thread Queueing

Rust closures can now be used to queue tasks onto the Qt event loop from a Rust background thread. This allows you to capture items rather than use a channel with function pointers, as we did in the previous release.

Thanks to Yuya Nishihara for this contribution.

// In an invokable request a handle to the qt thread
let qt_thread = self.qt_thread();
// Spawn a Rust thread
std::thread::spawn(move || {
    let value = compute_value_on_rust_thread();
    // Use a closure to move the value and run the task on the Qt event loop
    qt_thread
        .queue(move |mut qobject| {
            // Happens on the Qt event loop
            qobject.set_value(value);
        })
        .unwrap();
});

For More Information

Find CXX-Qt on our GitHub page.

We also have a Rust book with a getting started guide.

Discussions and contributions are welcome, as the API continues evolving for 0.5. With 0.5, we hope to improve the ability to implement listmodels from Rust and introduce Qt container types.

 

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: C++ / CMake / KDAB Blogs / KDAB on Qt / Qt / QtDevelopment / Rust / Technical / Tooling

Tags: / / / /
Leave a Reply

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