问题描述
.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" }
问题说明:
当我使用 QtCreator 编译上述程序时,显示以下错误:
错误:
file:///home/ppp/documents/test/build-qmlDeploy-Desktop_Qt_5_1_0_GCC_64bit-Debug/qrc:/main.qml:找不到文件
推荐答案
QUrl::fromLocalFile ()
根本不打算与 Qt Resource 一起使用,因为显然资源包 NOT 本地文件,而且 QML 文件不在 Qt 资源文件的根目录中,因此您必须在它之前添加路径:所以只需使用 QUrl ("qrc:/qml/qmlDeploy/main.qml")
!
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:如果你想避免长路径,像这样,只需将 .qrc
文件移动到与 QML 文件相同的目录中,你就会有更短的 URL.
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.
这篇关于Qt 资源中 QML 文件的文件未找到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!