Add more information in the README.md

master
Jos van den Oever 2017-09-04 00:29:08 +02:00
parent a28b26b669
commit ad62aa1530
4 changed files with 107 additions and 26 deletions

133
README.md
View File

@ -6,21 +6,47 @@ This code generator gets you started quickly to use Rust code from Qt and QML. I
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. 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](templates/qt_widgets) and one for [Qt Quick](templates/qt_quick). Just copy these folders as new project and start coding.
<table style="font-size: larger">
<tr>
<td style="background:#676767; color: white">Qt Widgets (main.cpp) / Qt Quick (main.qml)</td>
<td>&#10229; UI code, written by hand</td>
</tr>
<tr>
<td style="background:#3daefd">src/Binding.h</td>
<td rowspan="3" style="valign: center">&#10229; generated from binding.json</td>
</tr>
<tr>
<td style="background:#3daefd">src/Binding.cpp</td>
</tr>
<tr>
<td style="background:#3daefd">rust/src/interface.rs</td>
</tr>
<tr>
<td style="background:#676767; color: white">rust/src/implementation.rs</td>
<td>&#10229; Rust code, written by hand</td>
</tr>
</table>
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. 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.
```json ```json
{ {
"cppFile": "src/person.cpp", "cppFile": "src/Binding.cpp",
"rust": { "rust": {
"dir": "rust", "dir": "rust",
"interfaceModule": "interface", "interfaceModule": "interface",
"implementationModule": "implementation" "implementationModule": "implementation"
}, },
"objects": { "objects": {
"Person": { "Greeting": {
"type": "Object", "type": "Object",
"properties": { "properties": {
"name": { "message": {
"type": "QString", "type": "QString",
"write": true "write": true
} }
@ -30,7 +56,7 @@ To combine Qt and Rust, write an interface in a JSON file. From that, the genera
} }
``` ```
This file describes an binding with one object, `Person`. `Person` has one property: `name`. It is a writable property. 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: The Rust Qt Binding Generator will create binding source code from this description:
@ -40,45 +66,46 @@ rust_qt_binding_generator binding.json
This will create four files: This will create four files:
* *src/person.h* * *src/Binding.h*
* *src/person.cpp* * *src/Binding.cpp*
* rust/src/implementation.rs
* *rust/src/interface.rs* * *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. 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.
```rust ```rust
use interface::*; use interface::*;
/// A Person /// A Greeting
pub struct Person { pub struct Greeting {
/// Emit signals the the Qt code. /// Emit signals the the Qt code.
emit: PersonEmitter, emit: GreetingEmitter,
/// The name of the person. /// The message of the person.
name: String, message: String,
} }
/// Implementation of the binding /// Implementation of the binding
impl PersonTrait for Person { /// GreetingTrait is defined in interface.rs
/// Create a new person with default data. impl GreetingTrait for Greeting {
fn new(emit: PersonEmitter) -> Person { /// Create a new greeting with default data.
Person { fn new(emit: GreetingEmitter) -> Greeting {
Greeting {
emit: emit, emit: emit,
name: String::new(), message: "Hello World!",
} }
} }
/// The emitter can emit signals to the Qt code. /// The emitter can emit signals to the Qt code.
fn emit(&self) -> &PersonEmitter { fn emit(&self) -> &GreetingEmitter {
&self.emit &self.emit
} }
/// Get the name of the Person /// Get the message of the Greeting
fn get_name(&self) -> &str { fn get_message(&self) -> &str {
&self.name &self.message
} }
/// Set the name of the Person /// Set the message of the Greeting
fn set_name(&mut self, value: String) { fn set_message(&mut self, value: String) {
self.name = value; self.message = value;
self.emit.name_changed(); self.emit.message_changed();
} }
} }
``` ```
@ -87,6 +114,60 @@ The building block of Qt and QML projects are QObject and the Model View classes
This way, Rust code can be called from Qt and QML projects. This way, Rust code can be called from Qt and QML projects.
## More information ### Qt Widgets with Rust
This C++ code uses the Rust code written above.
```cpp
#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.
```qml
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.
<figure>
<img src="demo/screenshots/demo.png" alt="Qt Widgets UI with Rust logic"/>
<figcaption>Qt Widgets UI with Rust logic</figcaption>
</figure>
<figure>
<img src="demo/screenshots/demo.png" alt="Qt Quick Controls UI with Rust logic"/>
<figcaption>Qt Quick Controls UI with Rust logic</figcaption>
</figure>
<figure>
<img src="demo/screenshots/demo.png" alt="Qt Quick Controls 2 UI with Rust logic"/>
<figcaption>Qt Quick Controls 2 UI with Rust logic</figcaption>
</figure>
## 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
* [Qt](http://doc.qt.io/)
* [Qt Examples and tutorials](http://doc.qt.io/qt-5/qtexamplesandtutorials.html)
* [The QML Book](https://qmlbook.github.io/) * [The QML Book](https://qmlbook.github.io/)

BIN
demo/screenshots/demo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
demo/screenshots/demo2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

BIN
demo/screenshots/demo3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB