File not found error for a QML file inside a Qt resource

我与影子孤独终老i 提交于 2019-12-10 19:23:36

问题


.pro

# Add more folders to ship with the application, here
folder_01.source = qml/qmlDeploy
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01

TEMPLATE += app
QT += qml

# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp

# Please do not modify the following two lines. Required for deployment.
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()

RESOURCES += \
    resource.qrc

main.cpp

#include <QGuiApplication>
#include <QQuickView>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQuickView view;
    //view.setSource(QUrl::fromLocalFile("qml/qmlDeploy/main.qml"));
    view.setSource (QUrl::fromLocalFile("qrc:/main.qml"));
    view.show();

    return app.exec();
}

resource.qrc

<!DOCTYPE RCC>
<RCC version="1.0">

<qresource prefix="/">
    <file>qml/qmlDeploy/main.qml</file>
</qresource>

</RCC>

main.qml

import QtQuick 2.0

Rectangle { width: 100; height: 100; color: "red" }

Problem statement:

When I compile the above programs using QtCreator the following error is shown:
Error:

file:///home/ppp/documents/test/build-qmlDeploy-Desktop_Qt_5_1_0_GCC_64bit-Debug/qrc:/main.qml: File not found


回答1:


QUrl::fromLocalFile () is simply not meant to be used with Qt Resource, as obviously resource bundle is NOT local file, and also, QML file is not at the root of Qt resource file so you have to add the path before it : so just use QUrl ("qrc:/qml/qmlDeploy/main.qml") !

PS: If you want to avoid long paths, like this, just move the .qrc file in the same dir as QML files, and you will have shorter URL.




回答2:


I think that the problem is in this line:

view.setSource (QUrl::fromLocalFile("qrc:/main.qml"));

You have your bad URL syntax. You are trying to open a file in qrc: directory.

It should be like this:

view.setSource (QUrl("qrc:///qml/qmlDeploy/main.qml"));

Because qrc:// is the URL prefix, / is the root prefix, main.qml is the file inside it.



来源:https://stackoverflow.com/questions/23968058/file-not-found-error-for-a-qml-file-inside-a-qt-resource

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