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

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

问题描述

我在 Windows7 上用 c++ qnd Qt Creator(QML) 编写了一个 Qt Quick Desktop 应用程序.现在我必须部署它,我需要隐藏 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.)

我读到有一个很好的方法可以使用 .qrc 文件来做到这一点.我阅读了有关这些文件的文档,并为我的应用程序创建了一个,如下所示:

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>

在 main.cpp 中,我正在加载 Main.qml 文件,如下所示:

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

我尝试从 Resources.qrc 中读取 myMain.qml 文件,例如:

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

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

但我遇到了这个错误:

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

当我尝试这个时:

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: File not found

我的 Qt 似乎不知道如何处理:":/".

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

我必须使用 .qrc 文件:

I have to use the .qrc file in:

  • 加载 myMain.qml 文件
  • 使用导入到 myMain.qml 中的 qml 文件
  • 在我的 qml 文件中使用图片

你能解释一下有什么问题吗?为了在 c++ 和 qml 中使用 .qrc 我必须做什么?

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?

非常感谢:)

推荐答案

你试试这个:

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

当你这样做时:

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

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

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

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

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