Go to file
Jos van den Oever af7139027b Add a template for a Qt Widgets project 2017-09-03 22:07:49 +02:00
cmake Fix cmake find files so they work on debian and nixos 2017-09-01 21:11:30 +02:00
demo Remove unneeded resizing 2017-09-03 20:29:46 +02:00
docker Add missing qml charts module 2017-09-03 12:03:26 +02:00
src Show the rust type along with the Qt type in the error message 2017-09-03 20:48:07 +02:00
templates/qt_widgets Add a template for a Qt Widgets project 2017-09-03 22:07:49 +02:00
tests Rust api guidelines say constructors should be called "new" 2017-09-03 17:32:46 +02:00
.gitignore Add docker files for development environment 2017-09-03 11:15:57 +02:00
AUTHORS Add AUTHORS file 2017-08-25 18:19:05 +02:00
CMakeLists.txt Add appdata file 2017-09-03 19:06:24 +02:00
COPYING Add GPLv2+ licenses 2017-08-30 23:35:26 +02:00
Messages.sh Prepare hte demo application for the KDE i18n system 2017-09-02 23:47:00 +02:00
README.md Rust api guidelines say constructors should be called "new" 2017-09-03 17:32:46 +02:00
design.txt Separate interface from implementation 2017-08-03 11:18:52 +02:00
org.kde.rust_qt_binding_generator.appdata.xml Add appdata file 2017-09-03 19:06:24 +02:00

README.md

Rust Qt Binding Generator

Rust Qt Binding

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.

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/person.cpp",
    "rust": {
        "dir": "rust",
        "interfaceModule": "interface",
        "implementationModule": "implementation"
    },
    "objects": {
        "Person": {
            "type": "Object",
            "properties": {
                "name": {
                    "type": "QString",
                    "write": true
                }
            }
        }
    }
}

This file describes an binding with one object, Person. Person has one property: name. 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:

  • src/person.h
  • src/person.cpp
  • rust/src/implementation.rs
  • rust/src/interface.rs

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 Person
pub struct Person {
    /// Emit signals  the the Qt code.
    emit: PersonEmitter,
    /// The name of the person.
    name: String,
}

/// Implementation of the binding
impl PersonTrait for Person {
    /// Create a new person with default data.
    fn new(emit: PersonEmitter) -> Person {
        Person {
            emit: emit,
            name: String::new(),
        }
    }
    /// The emitter can emit signals to the Qt code.
    fn emit(&self) -> &PersonEmitter {
        &self.emit
    }
    /// Get the name of the Person
    fn get_name(&self) -> &str {
        &self.name
    }
    /// Set the name of the Person
    fn set_name(&mut self, value: String) {
        self.name = value;
        self.emit.name_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.

More information