Add a template that is built by Cargo

master
Jos van den Oever 2018-10-27 21:18:54 +02:00
parent c1a0bd7bfb
commit ed2de05492
10 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,11 @@
[package]
name = "qt_quick_cargo"
version = "0.1.0"
build = "build.rs"
links = "qt_quick_cargo"
[dependencies]
libc = "*"
[build-dependencies]
rust_qt_binding_generator = { path = "../.." }

View File

@ -0,0 +1,34 @@
import QtQuick 2.6
import RustCode 1.0
Rectangle {
property alias mouseArea: mouseArea
property alias textEdit: textEdit
Simple {
id: rust
}
width: 360
height: 360
MouseArea {
id: mouseArea
anchors.fill: parent
}
TextEdit {
id: textEdit
text: rust.message
verticalAlignment: Text.AlignVCenter
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
anchors.topMargin: 20
Rectangle {
anchors.fill: parent
anchors.margins: -10
color: "transparent"
border.width: 1
}
}
}

View File

@ -0,0 +1,13 @@
Qt Quick template project with Rust bindings
This is a template project for writing a Qt Quick GUI on top of Rust code.
`bindings.json` defines the interface between the Qt and Rust code.
Build instructions are written in `build.rs`.
Build this code with
```bash
cargo build
```

View File

@ -0,0 +1,19 @@
{
"cppFile": "Bindings.cpp",
"rust": {
"dir": "",
"interfaceModule": "interface",
"implementationModule": "implementation"
},
"objects": {
"Simple": {
"type": "Object",
"properties": {
"message": {
"type": "QString",
"write": true
}
}
}
}
}

View File

@ -0,0 +1,10 @@
extern crate rust_qt_binding_generator;
fn main() {
let out_dir = ::std::env::var("OUT_DIR").unwrap();
rust_qt_binding_generator::build::Build::new(&out_dir)
.bindings("bindings.json")
.qrc("qml.qrc")
.cpp("src/main.cpp")
.compile("qt_quick_cargo");
}

View File

@ -0,0 +1,16 @@
import QtQuick 2.6
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MainForm {
anchors.fill: parent
mouseArea.onClicked: {
console.log(qsTr('Clicked on background. Text: "' + textEdit.text + '"'))
}
}
}

View File

@ -0,0 +1,6 @@
<RCC>
<qresource prefix="/">
<file>main.qml</file>
<file>MainForm.ui.qml</file>
</qresource>
</RCC>

View File

@ -0,0 +1,26 @@
use interface::*;
pub struct Simple {
emit: SimpleEmitter,
message: String,
}
impl SimpleTrait for Simple {
fn new(emit: SimpleEmitter) -> Simple {
Simple {
emit: emit,
message: String::new(),
}
}
fn emit(&mut self) -> &mut SimpleEmitter {
&mut self.emit
}
fn message(&self) -> &str {
"Hello World!"
}
fn set_message(&mut self, value: String) {
self.message = value;
self.emit.message_changed();
}
}

View File

@ -0,0 +1,29 @@
#include "Bindings.h"
#include <QtCore/QFile>
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/qqml.h>
extern "C" {
int main_cpp(const char* app);
}
int main_cpp(const char* appPath)
{
int argc = 1;
char* argv[1] = { (char*)appPath };
QGuiApplication app(argc, argv);
qmlRegisterType<Simple>("RustCode", 1, 0, "Simple");
QQmlApplicationEngine engine;
if (QFile("main.qml").exists()) {
engine.load(QUrl(QStringLiteral("main.qml")));
} else {
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
}
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}

View File

@ -0,0 +1,20 @@
extern crate libc;
mod implementation;
pub mod interface {
include!(concat!(env!("OUT_DIR"), "/src/interface.rs"));
}
use std::os::raw::c_char;
extern {
fn main_cpp(app: *const c_char);
}
fn main() {
use std::ffi::CString;
let mut args = ::std::env::args();
let app = CString::new(args.next().unwrap()).unwrap();
unsafe {
main_cpp(app.as_ptr());
}
}