Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use qrc in c++ and QML application?

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:)

like image 473
user1835297 Avatar asked Nov 26 '12 15:11

user1835297


1 Answers

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"

like image 127
Kirween Avatar answered Sep 22 '22 19:09

Kirween