diff --git a/templates/qt_quick_cargo/Cargo.toml b/templates/qt_quick_cargo/Cargo.toml new file mode 100644 index 0000000..f1337f3 --- /dev/null +++ b/templates/qt_quick_cargo/Cargo.toml @@ -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 = "../.." } diff --git a/templates/qt_quick_cargo/MainForm.ui.qml b/templates/qt_quick_cargo/MainForm.ui.qml new file mode 100644 index 0000000..9cbb5e0 --- /dev/null +++ b/templates/qt_quick_cargo/MainForm.ui.qml @@ -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 + } + } +} diff --git a/templates/qt_quick_cargo/README.md b/templates/qt_quick_cargo/README.md new file mode 100644 index 0000000..f0befce --- /dev/null +++ b/templates/qt_quick_cargo/README.md @@ -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 +``` diff --git a/templates/qt_quick_cargo/bindings.json b/templates/qt_quick_cargo/bindings.json new file mode 100644 index 0000000..fe68494 --- /dev/null +++ b/templates/qt_quick_cargo/bindings.json @@ -0,0 +1,19 @@ +{ + "cppFile": "Bindings.cpp", + "rust": { + "dir": "", + "interfaceModule": "interface", + "implementationModule": "implementation" + }, + "objects": { + "Simple": { + "type": "Object", + "properties": { + "message": { + "type": "QString", + "write": true + } + } + } + } +} diff --git a/templates/qt_quick_cargo/build.rs b/templates/qt_quick_cargo/build.rs new file mode 100644 index 0000000..0192dbf --- /dev/null +++ b/templates/qt_quick_cargo/build.rs @@ -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"); +} diff --git a/templates/qt_quick_cargo/main.qml b/templates/qt_quick_cargo/main.qml new file mode 100644 index 0000000..6d8bd40 --- /dev/null +++ b/templates/qt_quick_cargo/main.qml @@ -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 + '"')) + } + } +} diff --git a/templates/qt_quick_cargo/qml.qrc b/templates/qt_quick_cargo/qml.qrc new file mode 100644 index 0000000..7684346 --- /dev/null +++ b/templates/qt_quick_cargo/qml.qrc @@ -0,0 +1,6 @@ + + + main.qml + MainForm.ui.qml + + diff --git a/templates/qt_quick_cargo/src/implementation.rs b/templates/qt_quick_cargo/src/implementation.rs new file mode 100644 index 0000000..ddd40e8 --- /dev/null +++ b/templates/qt_quick_cargo/src/implementation.rs @@ -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(); + } +} + diff --git a/templates/qt_quick_cargo/src/main.cpp b/templates/qt_quick_cargo/src/main.cpp new file mode 100644 index 0000000..3fc8e2e --- /dev/null +++ b/templates/qt_quick_cargo/src/main.cpp @@ -0,0 +1,29 @@ +#include "Bindings.h" + +#include +#include +#include +#include + +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("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(); +} diff --git a/templates/qt_quick_cargo/src/main.rs b/templates/qt_quick_cargo/src/main.rs new file mode 100644 index 0000000..4098c12 --- /dev/null +++ b/templates/qt_quick_cargo/src/main.rs @@ -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()); + } +}