Iet uz failu
an 2a8664e9bf add more trivial types 2019-07-10 06:05:58 -04:00
cmake Sync FindCargo.cmake with that of kdev-cargo 2017-12-22 18:30:17 +01:00
demo Add placeholder variable name in generated code 2019-02-13 07:57:25 +01:00
docker Upgrade Docker image to latest Ubuntu version 2018-04-30 16:50:15 +02:00
examples/todos Fix building with MSVC 2018-11-11 16:03:29 +01:00
src add more trivial types 2019-07-10 06:05:58 -04:00
templates Fix building with MSVC 2018-11-11 16:03:29 +01:00
tests Add placeholder variable name in generated code 2019-02-13 07:57:25 +01:00
tutorial Add make to the Docker image 2018-02-07 12:31:21 +01:00
.arcconfig Add .arcconfig 2017-12-16 11:18:50 +01:00
.gitignore Port rust_qt_binding_generator from C++ to Rust 2018-09-28 12:11:57 +02:00
AUTHORS add base class property for objects 2019-07-03 22:33:40 -04:00
CMakeLists.txt Build even when CMAKE_DL_LIBS is not defined 2018-10-09 00:05:29 +02:00
COPYING Add GPLv2+ licenses 2017-08-30 23:35:26 +02:00
COPYING.EXCEPTION Add line breaks 2018-05-18 20:07:45 +02:00
Cargo.toml Update version and ChangeLog 2019-02-25 23:47:54 +01:00
ChangeLog Update version and ChangeLog 2019-02-25 23:47:54 +01:00
README.md Add a link to a presentation 2018-09-25 21:49:20 +02:00
design.txt Separate interface from implementation 2017-08-03 11:18:52 +02:00
org.kde.rust_qt_binding_generator.appdata.xml GIT_SILENT made messages (after extraction) 2019-05-20 02:52:42 +02:00
org.kde.rust_qt_binding_generator.desktop SVN_SILENT made messages (.desktop file) - always resolve ours 2019-06-23 05:04:17 +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.

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.

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:

  • src/Binding.h
  • src/Binding.cpp
  • rust/src/interface.rs
  • rust/src/implementation.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 Greeting
pub struct Greeting {
    /// Emit signals to the Qt code.
    emit: GreetingEmitter,
    /// The message of the person.
    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!",
        }
    }
    /// 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 on Qt

Tutorials

Presentation

Information on Qt

Issues

Please report bugs and feature requests in the KDE issue tracker, product "rust-qt-binding-generator".