White-Oak / qml-rust

QML (Qt Quick) bindings for Rust language
MIT License
205 stars 18 forks source link

Allow loading from resource #1

Closed nbigaouette closed 7 years ago

nbigaouette commented 7 years ago

The QmlEngine::load_file() method assumes a file:/// protocol (see qmlengine.rs line 51).

This breaks loading a QML file from the resource system using qrc:/ "protocol".

The protocol should be left to the user imo...

DOtherSide supports qrc:/, see its test_dotherside.cpp line 263.

nbigaouette commented 7 years ago

Note that I need this feature to load my main.qml file. I load it for now using a plain path.

I want to include all my QML files as resources so I do not depend on running the application in the source folder. As such, I include them all as Qt resources. For this, I created a .pro file with the following:

TEMPLATE = lib
RESOURCES = myapp.qrc
CONFIG += staticlib

and place all my QML files in the resource file myapp.qrc as such:

<RCC>
    <qresource prefix="/">
        <file>qml/Component1.qml</file>
        <file>qml/Component2.qml</file>
       ...
    </qresource>
</RCC>

Then, a build.rs file tells rustc to run qmake on that Qt project file, which creates a static library containing all QML files as resources. I just need to link that lib to the rust binary (again, done in build.rs).

Being able to load using qrc:/ is required for my code since I use a singleton to dispatch some signals.

This singleton "looses" its singleton nature if I load files through qrc:/ and directly. I can see this by printing this from a method in the QML singleton: I will get two different addresses...

White-Oak commented 7 years ago

I'll get it done soon, thanks for the issue!

And big thanks for an example of qrc/pro file to use qmls as resources!

White-Oak commented 7 years ago

@nbigaouette I've added a load_url to load from a specified url, can you test this out?

I'm going to start adding tests soon, but currently needs to be tested manually.

nbigaouette commented 7 years ago

Great thanks it works! I haven't tried the other protocols (file:// and http://) for qrc:/ works:

let mut engine = qml::QmlEngine::new();
engine.load_url("qrc:///qml/main.qml");