Rust Qt Binding Generator

2017-09-03

This blog post is the announcement of Rust Qt Binding Generator. The project is under review for inclusion in KDE. You can get the source code here.

Scroll to the bottom for the screenshots.

Rust Qt Binding Generator (Logo by Alessandro Longo)

Rust Qt Binding Generator (Logo by Alessandro Longo)

This code generator gets you started quickly to use Rust code from Qt and QML. In other words, it helps to create a Qt based GUI on top of Rust code.

Qt is a mature cross-platform graphical user interface library. Rust is a new programming language with strong compile time checks and a modern syntax.

Getting started

There are two template projects that help you to get started quickly. One for Qt Widgets and one for Qt Quick. Just copy these folders as new project and start coding.

Here is a small schematic of how files made by the generator are related:

Qt Widgets (main.cpp) / Qt Quick (main.qml)

⟵ UI code, written by hand

src/Binding.h

⟵ generated from
binding.json

src/Binding.cpp

rust/src/interface.rs

rust/src/implementation.rs

⟵ Rust code, written by hand

To combine Qt and Rust, write an interface in a JSON file. From that, the generator creates Qt code and Rust code. The Qt code can be used directly. The Rust code has two files: interface and implementation. The interface can be used directly.

{
    "cppFile": "src/Binding.cpp",
    "rust": {
        "dir": "rust",
        "interfaceModule": "interface",
        "implementationModule": "implementation"
    },
    "objects": {
        "Greeting": {
            "type": "Object",
            "properties": {
                "message": {
                    "type": "QString",
                    "write": true
                }
            }
        }
    }
}

This file describes an binding with one object, Greeting. Greeting has one property: message. It is a writable property.

The Rust Qt Binding Generator will create binding source code from this description:

rust_qt_binding_generator binding.json

This will create four files:

Only implementation.rs should be changed. The other files are the binding. implementation.rs is initialy created with a simple implementation that is shown here with some comments.

use interface::*;

/// A Greeting
pub struct Greeting {
    /// Emit signals to the Qt code.
    emit: GreetingEmitter,
    /// The message of the greeting.
    message: String,
}

/// Implementation of the binding
/// GreetingTrait is defined in interface.rs
impl GreetingTrait for Greeting {
    /// Create a new greeting with default data.
    fn new(emit: GreetingEmitter) -> Greeting {
        Greeting {
            emit: emit,
            message: "Hello World!".into(),
        }
    }
    /// The emitter can emit signals to the Qt code.
    fn emit(&self) -> &GreetingEmitter {
        &self.emit
    }
    /// Get the message of the Greeting
    fn message(&self) -> &str {
        &self.message
    }
    /// Set the message of the Greeting
    fn set_message(&mut self, value: String) {
        self.message = value;
        self.emit.message_changed();
    }
}

The building block of Qt and QML projects are QObject and the Model View classes. rust_qt_binding_generator reads a json file to generate QObject or QAbstractItemModel classes that call into generated Rust files. For each type from the JSON file, a Rust trait is generated that should be implemented.

This way, Rust code can be called from Qt and QML projects.

Qt Widgets with Rust

This C++ code uses the Rust code written above.

#include "Binding.h"
#include <QDebug>
int main() {
    Greeting greeting;
    qDebug() << greeting.message();
    return 0;
}

Qt Quick with Rust

This Qt Quick (QML) code uses the Rust code written above.

Rectangle {
    Greeting {
        id: rust
    }
    Text {
        text: rust.message
    }
}

Demo application

The project comes with a demo application that show a Qt user interface based on Rust. It uses all of the features of Object, List and Tree. Reading the demo code is a good way to get started.

Qt Widgets UI with Rust logic
Qt Widgets UI with Rust logic
Qt Quick Controls UI with Rust logic
Qt Quick Controls UI with Rust logic
Qt Quick Controls 2 UI with Rust logic
Qt Quick Controls 2 UI with Rust logic

Docker development environment

To get started quickly, the project comes with a Dockerfile. You can start a docker session with the required dependencies with ./docker/docker-bash-session.sh.

More information

Comments

Post a comment

Rust Qt Binding Generator

Really cool project. I tested the code in your repo and it compiled flawless. I hope i can find some time on the weekends to dig a little deeper and try some little examples myself.

Reply to this comment

Rust Qt Binding Generator

That's great to hear. A KDE contributor managed to run in FreeBSD. I've not heard from people targeting Mac OS X, Windows, Android or iOS yet, but this approach should also work on those platforms.

Reply to this comment

Rust Qt Binding Generator

Great to see some more activity on Qt <> Rust! Some questions:

* Is there a public bugtracker?

* Is there any relationship with https://github.com/rust-qt/cpp_to_rust and https://github.com/White-Oak/qml-rust ? How does it compare (feature-set, technical choices...)?

* I heard there was some complexity around implemeting some advanced Qt features related to signals/slots and custom Qml classes, did this lib solve them?

Reply to this comment

Rust Qt Binding Generator

* Is there a public bugtracker?

Not yet, it's requested and will appear at https://bugs.kde.org

* Is there any relationship with cpp_to_rust and qml-rust?

None.

* I heard there was some complexity...

This binding generator is meant exactly to create custom QObjects that are implemented in Rust. In fact, that is all it does. The UI of your application will be writting in QML or C++. This lets you use each language where it shines. Qt and QML for writing nice user interface and Rust for the hard work.

Reply to this comment

Rust Qt Binding Generator

This is pretty nice, I personally could probably work with that, but I am pretty sure the "JSON" indirection could be removed by using Rust macros 1.1/2.0. I would like to look into the project, but it not being on GitHub, for some reason, is a big repellent for me(new account, limited permissions, limited 3rd party services support, weird workflows, etc...)... :(

Reply to this comment

Rust Qt Binding Generator

Using Rust macros might come. I would certainly consider patches that port to using Rust macros with interest. Personally, I find the current solution of generating from JSON to C++ and Rust with one executable good enough. It's easy enough to add that to your build system.

The KDE community where this project is hosted, is dedicated to creating free software. I am more comfortable with primary hosting at KDE. The code is easy to get via the git url. It is mirrored on GitHub. Patches and bug reports are accepted via the KDE infrastructure.

Reply to this comment

Rust Qt Binding Generator

Came here from golang reddit.

I am planning on making some GUI apps for linux and maybe windows as well. The only real choice until now was to use C/C++. But this project looks pretty good. Time to learn Rust I guess.

Reply to this comment