How to use qrc in c++ and QML application?
Asked Answered
D

1

13

I have written a Qt Quick Desktop application in c++ qnd Qt Creator(QML) on Windows7. Now I have to deploy it, and I need to hide the qml files and the images(means: to put them in resources and etc.)

I've read that there is a great way to do that with .qrc files. I read the documentation about those files, and created one for my application, which looks like this:

<RCC>
<qresource prefix="/">
    <file>qml/GenericHostApplicationQML/myMain.qml</file>
    <file>qml/GenericHostApplicationQML/content/PressAndHoldButton.qml</file>
    <file>qml/GenericHostApplicationQML/content/TextButton.qml</file>
    <file>qml/GenericHostApplicationQML/content/pics/advancedsettings.png</file>
    <file>qml/GenericHostApplicationQML/content/pics/cnruninstall.png</file>
    <file>qml/GenericHostApplicationQML/content/pics/dialog_cancel.png</file>
    <file>qml/GenericHostApplicationQML/content/pics/folder_explore.png</file>
    <file>qml/GenericHostApplicationQML/content/pics/gnome_session_switch.png</file>
    <file>qml/GenericHostApplicationQML/content/pics/mail2_send.png</file>
    <file>qml/GenericHostApplicationQML/content/pics/Picture1.png</file>
    <file>qml/GenericHostApplicationQML/content/pics/Picture2.png</file>
</qresource>

In the main.cpp, I'm loading the Main.qml file like:

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);
   QDeclarativeView view;
   view.setSource(QUrl::fromLocalFile("qml/GenericHostApplicationQML/myMain.qml"));
   view.show();
   return app.exec();
}

I tried to read the myMain.qml file from the Resources.qrc like:

view.setSource(QUrl(":/qml/GenericHostApplicationQML/myMain.qml"));//I added the ":/"

but I've got this error:

file:///qml/GenericHostApplicationQML/myMain.qml: File not found 

and when I tried this:

view.setSource(QUrl::fromLocalFile(":/qml/GenericHostApplicationQML/myMain.qml"));

I'm getting this:

file:///C:/Users/ayalafre/Desktop/ghaQML/GenericHostApplicationQML-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Release/:/qml/GenericHostApplicationQML/myMain.qml: File not found

It seems like my Qt doesn't know what to do with:":/".

I have to use the .qrc file in:

  • Loading the myMain.qml file
  • Using import to qml files in myMain.qml
  • Using images in my qml files

Could you explain to me what's wrong?? and what I have to do in order to use the .qrc in both c++ and qml?

Thanks a lot:)

Dank answered 26/11, 2012 at 15:57 Comment(1)
i'm pretty sure there is no way to actually hide QML code, even if you use the resource file, that code is still visible to anyone who inspects your executable, i.e. it is not compiled, encrypted or turned into binary code in any way.Madrepore
D
24

Have you try this:

view.setSource(QUrl("qrc:/qml/GenericHostApplicationQML/myMain.qml"));

When you do this:

QUrl(":/qml/GenericHostApplicationQML/myMain.qml");

the path is "file:///qml/GenericHostApplicationQML/myMain.qml"

Declivous answered 26/11, 2012 at 16:23 Comment(4)
Oh Tnx, It works!!! and should you guide me please how to use it in qml image source and in qml import? Thanks ahead!!Dank
you want to read directly the qml file from local source file and not from the resources file ?Declivous
I don't understand what do you mean, I'll explain what I mean to: I have a PressAndHoldButton.qml file, and I want to use it in myMain.qml file, for example: PressAndHoldButton{ source: "qrc:/qml/GenericHostApplicationQML/content/pics/dialog_cancel.png" y: 647 x: 250 enabled: !page.isMessageMode onClicked:menu.clearOutput(); } How can I cause the myMain.qml file to know the PressAndHoldButton.qml file, from the .qrc file?Dank
doc.qt.digia.com/qt/qdeclarativemodules.html#located-modules something like this: import "qml/GenericHostApplicationQML" Item { PressAndHoldButton { ... } } ...Declivous

© 2022 - 2024 — McMap. All rights reserved.