Warning, /sdk/rust-qt-binding-generator/README.md is written in an unsupported language. File is not indexed.

0001 # Rust Qt Binding Generator
0002 
0003 ![Rust Qt Binding](demo/rust_qt_binding_generator.svg)
0004 
0005 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.
0006 
0007 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.
0008 
0009 ## Getting started
0010 
0011 There are two template projects that help you to get started quickly. One for [Qt Widgets](templates/qt_widgets) and one for [Qt Quick](templates/qt_quick). Just copy these folders as new project and start coding.
0012 
0013 
0014 <table style="font-size: larger">
0015  <tr>
0016   <td style="background:#676767; color: white">Qt Widgets (main.cpp) / Qt Quick (main.qml)</td>
0017   <td>&#10229; UI code, written by hand</td>
0018  </tr>
0019  <tr>
0020   <td style="background:#3daefd">src/Binding.h</td>
0021   <td rowspan="3" style="valign: center">&#10229; generated from binding.json</td>
0022  </tr>
0023  <tr>
0024   <td style="background:#3daefd">src/Binding.cpp</td>
0025  </tr>
0026  <tr>
0027   <td style="background:#3daefd">rust/src/interface.rs</td>
0028  </tr>
0029  <tr>
0030   <td style="background:#676767; color: white">rust/src/implementation.rs</td>
0031   <td>&#10229; Rust code, written by hand</td>
0032  </tr>
0033 </table>
0034 
0035 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.
0036 
0037 ```json
0038 {
0039     "cppFile": "src/Binding.cpp",
0040     "rust": {
0041         "dir": "rust",
0042         "interfaceModule": "interface",
0043         "implementationModule": "implementation"
0044     },
0045     "objects": {
0046         "Greeting": {
0047             "type": "Object",
0048             "properties": {
0049                 "message": {
0050                     "type": "QString",
0051                     "write": true
0052                 }
0053             }
0054         }
0055     }
0056 }
0057 ```
0058 
0059 This file describes an binding with one object, `Greeting`. `Greeting` has one property: `message`. It is a writable property.
0060 
0061 The Rust Qt Binding Generator will create binding source code from this description:
0062 
0063 ```bash
0064 rust_qt_binding_generator binding.json
0065 ```
0066 
0067 This will create four files:
0068 
0069 * *src/Binding.h*
0070 * *src/Binding.cpp*
0071 * *rust/src/interface.rs*
0072 * rust/src/implementation.rs
0073 
0074 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.
0075 
0076 ```rust
0077 use interface::*;
0078 
0079 /// A Greeting
0080 pub struct Greeting {
0081     /// Emit signals to the Qt code.
0082     emit: GreetingEmitter,
0083     /// The message of the person.
0084     message: String,
0085 }
0086 
0087 /// Implementation of the binding
0088 /// GreetingTrait is defined in interface.rs
0089 impl GreetingTrait for Greeting {
0090     /// Create a new greeting with default data.
0091     fn new(emit: GreetingEmitter) -> Greeting {
0092         Greeting {
0093             emit: emit,
0094             message: "Hello World!",
0095         }
0096     }
0097     /// The emitter can emit signals to the Qt code.
0098     fn emit(&self) -> &GreetingEmitter {
0099         &self.emit
0100     }
0101     /// Get the message of the Greeting
0102     fn message(&self) -> &str {
0103         &self.message
0104     }
0105     /// Set the message of the Greeting
0106     fn set_message(&mut self, value: String) {
0107         self.message = value;
0108         self.emit.message_changed();
0109     }
0110 }
0111 ```
0112 
0113 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.
0114 
0115 This way, Rust code can be called from Qt and QML projects.
0116 
0117 ### Qt Widgets with Rust
0118 
0119 This C++ code uses the Rust code written above.
0120 
0121 ```cpp
0122 #include "Binding.h"
0123 #include <QDebug>
0124 int main() {
0125     Greeting greeting;
0126     qDebug() << greeting.message();
0127     return 0;
0128 }
0129 ```
0130 
0131 ### Qt Quick with Rust
0132 
0133 This Qt Quick (QML) code uses the Rust code written above.
0134 
0135 ```qml
0136 Rectangle {
0137     Greeting {
0138         id: rust
0139     }
0140     Text {
0141         text: rust.message
0142     }
0143 }
0144 ```
0145 
0146 ## Demo application
0147 
0148 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.
0149 
0150 <figure>
0151   <img src="demo/screenshots/demo.png" alt="Qt Widgets UI with Rust logic"/>
0152   <figcaption><a href="demo/src/main.cpp">Qt Widgets UI</a> with <a href="demo/rust/src/implementation/file_system_tree.rs">Rust logic</a></figcaption>
0153 </figure>
0154 
0155 <figure>
0156   <img src="demo/screenshots/demo2.png" alt="Qt Quick Controls UI with Rust logic"/>
0157   <figcaption><a href="demo/qml/DataAndChart.qml">Qt Quick Controls UI</a> with <a href="demo/rust/src/implementation/time_series.rs">Rust logic</a></figcaption>
0158 </figure>
0159 
0160 <figure>
0161   <img src="demo/screenshots/demo3.png" alt="Qt Quick Controls 2 UI with Rust logic"/>
0162   <figcaption><a href="demo/qml/FileTreeView2.qml">Qt Quick Controls 2 UI</a> with <a href="demo/rust/src/implementation/file_system_tree.rs">Rust logic</a></figcaption>
0163 </figure>
0164 
0165 ## Docker development environment
0166 
0167 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`.
0168 
0169 ## More information on Qt
0170 
0171 * [Initial blog post](https://www.vandenoever.info/blog/2017/09/04/rust_qt_binding_generator.html)
0172 
0173 ### Tutorials
0174 
0175 * [Rust and QML: a timely example](https://www.vandenoever.info/blog/2017/09/10/time_for_rust_and_qml.html)
0176 * [To do a Rust GUI](https://www.vandenoever.info/blog/2018/06/09/to-do-a-rust-gui.html)
0177 * [Browsing your mail with Rust and Qt](https://www.vandenoever.info/blog/2018/09/16/browsing_your_mail_with_rust_and_qt.html)
0178 
0179 ### Presentation
0180 
0181 * [Qt GUIs with Rust](https://archive.fosdem.org/2018/schedule/event/rust_qt_binding_generator/)
0182 
0183 ### Information on Qt
0184 
0185 * [Qt](http://doc.qt.io/)
0186 * [Qt Examples and tutorials](http://doc.qt.io/qt-5/qtexamplesandtutorials.html)
0187 * [The QML Book](https://qmlbook.github.io/)
0188 
0189 ## Issues
0190 
0191 Please [report bugs and feature requests](https://bugs.kde.org/enter_bug.cgi?product=rust-qt-binding-generator) in the KDE issue tracker, product "rust-qt-binding-generator".