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.
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.
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 |
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.
: GreetingEmitter,
emit/// The message of the greeting.
: String,
message}
/// 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: "Hello World!".into(),
message}
}
/// 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.
This C++ code uses the Rust code written above.
#include "Binding.h"
#include <QDebug>
int main() {
;
Greeting greetingqDebug() << greeting.message();
return 0;
}
This Qt Quick (QML) code uses the Rust code written above.
Rectangle {
Greeting {id: rust
}Text {
text: rust.message
} }
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.
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
.
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.
By Dustin at Mon, 04 Sep 2017 10:52:39 +0200
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.
By Jos van den Oever at Mon, 04 Sep 2017 11:12:02 +0200
Rust Qt Binding Generator
I tried building on Windows and since Windows doesn't have pthreads it fails to build.
Here is the error from CMakeError.log
C:\Users\wasim\Documents\src\rust-qt-binding-generator\build\CMakeFiles\CMakeTmp\CheckIncludeFile.c(1): fatal error C1083: Cannot open include file: 'pthread.h': No such file or directory [C:\Users\wasim\Documents\src\rust-qt-binding-generator\build\CMakeFiles\CMakeTmp\cmTC_5e7c3.vcxproj] Done Building Project "C:\Users\wasim\Documents\src\rust-qt-binding-generator\build\CMakeFiles\CMakeTmp\cmTC_5e7c3.vcxproj" (default targets) -- FAILED.
By Adnan at Sat, 20 Jan 2018 01:55:27 +0100
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?
By EdwardO at Thu, 07 Sep 2017 10:50:51 +0200
Rust Qt Binding Generator
Not yet, it's requested and will appear at https://bugs.kde.org
None.
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.
By Jos van den Oever at Thu, 07 Sep 2017 15:50:50 +0200
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...)... :(
By Lilian at Fri, 08 Sep 2017 22:38:15 +0200
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.
By Jos van den Oever at Sun, 10 Sep 2017 16:22:26 +0200
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.
By Ishan Jain at Mon, 18 Sep 2017 14:24:41 +0200