GithubHelp home page GithubHelp logo

rubdos / qmlrs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cyndis/qmlrs

0.0 2.0 0.0 375 KB

QtQuick interface for Rust

License: Apache License 2.0

Rust 53.83% CMake 2.89% C++ 42.95% QML 0.33%

qmlrs's Introduction

qmlrs - QtQuick bindings for Rust

Image of example

qmlrs allows the use of Qml/QtQuick code from Rust, specifically

  • Rust code can create a QtQuick engine (QQmlApplicationEngine) with a loaded Qml script
  • Qml code can invoke Rust functions

..with certain limitations. The library should be safe (as in not unsafe) to use, but no promises at this time. Reviews of the code would be welcome.

Requirements

The library consists of a Rust part and a C++ part. The C++ part will be compiled automatically when building with Cargo. You will need cmake, Qt5 and a C++ compiler that can compile Qt5 code. Your Qt5 installation should have at least the following modules: Core, Gui, Qml, Quick and Quick Controls.

If you are installing Qt5 from source, please note that passing "-noaccessibility" to the configure script disables the qtquickcontrols module.

Usage

If you want to use qmlrs add the following lines to your Cargo.toml:

[dependencies.qmlrs]
git = "git://github.com/cyndis/qmlrs.git"

Example

This is the Rust code for an application allowing the calculation of factorials. (Also contains a test for signals.) You can find the corresponding Qml code in the examples directory.

#![feature(core)]

#[macro_use]
extern crate qmlrs;

struct Factorial;
impl Factorial {
    fn calculate(&self, x: i64) -> i64 {
        std::iter::range_inclusive(1, x).fold(1, |t,c| t * c)
    }
}

Q_OBJECT! { Factorial:
    slot fn calculate(i64);
}

fn main() {
    let mut engine = qmlrs::Engine::new();

    engine.set_property("factorial", Factorial);
    engine.load_local_file("examples/factorial_ui.qml");

    engine.exec();
}

To run the above example, execute cargo run --example factorial in the project's root directory.

Note regarding the Qt event loop and threads

Creating an Engine automatically initializes the Qt main event loop if one doesn't already exist. At least on some operating systems, the event loop must run on the main thread. Qt will tell you if you mess up. The .exec() method on views starts the event loop. This will block the thread until the window is closed.

Licensing

The code in this library is dual-licensed under the MIT license and the Apache License (version 2.0). See LICENSE-APACHE and LICENSE-MIT for details.

qmlrs's People

Contributors

cyndis avatar liamsi avatar florianjacob avatar wlentz avatar benaryorg avatar kirillkh avatar

Watchers

James Cloos avatar Ruben De Smet avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.