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

寵の児 提交于 2019-12-06 20:14:23

问题


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


回答1:


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"



来源:https://stackoverflow.com/questions/13568437/how-to-use-qrc-in-c-and-qml-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!