如何在c ++和QML应用程序中使用qrc? [英] How to use qrc in c++ and QML application?

查看:541
本文介绍了如何在c ++和QML应用程序中使用qrc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows7上用c ++ qnd Qt Creator(QML)编写了一个Qt Quick Desktop应用程序。现在
我必须部署它,我需要隐藏qml文件和图像(意味着:把它们放在资源等)。



我'读了,有一个伟大的方法来做这个.qrc文件。我读了关于这些文件的文档,并为我的应用程序创建了一个,它看起来像这样:

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





我加载Main.qml文件像:

  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();
}

我试图从Resources.qrc中读取myMain.qml文件:

  view.setSource(QUrl(:/ qml / GenericHostApplicationQML / myMain.qml)); //我添加了 :/

但我遇到此错误:

  file:///qml/GenericHostApplicationQML/myMain.qml:找不到文件

,当我尝试这个时:

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

我得到这个:

  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:找不到文件

似乎我的Qt不知道该怎么办::/。



我必须使用.qrc文件:




  • 加载myMain.qml文件

  • 在myMain.qml中使用导入qml文件

  • 在我的qml文件中使用图片



你能向我解释什么是错误吗?
和为了在c ++和qml中使用.qrc,我必须做什么?



非常感谢。)

解决方案

您尝试过:

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

执行此操作时:

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

路径是file:///qml/GenericHostApplicationQML/myMain.qml


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

解决方案

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"

这篇关于如何在c ++和QML应用程序中使用qrc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆