Qt资源中QML文件的“找不到文件”错误
File not found error for a QML file inside a Qt resource
.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: File not found
QUrl::fromLocalFile ()
根本不打算与Qt Resource一起使用,因为显然资源捆绑包是not本地文件,而且QML文件不在Qt资源文件的根目录下,所以您必须在它之前添加路径:所以只需使用QUrl ("qrc:/qml/qmlDeploy/main.qml")
!
PS:如果你想避免像这样的长路径,只需将.qrc
文件移动到与QML文件相同的目录中,你就会有更短的URL
我认为问题出在这一行:
view.setSource (QUrl::fromLocalFile("qrc:/main.qml"));
您的URL语法不正确。您正试图打开qrc:
目录中的一个文件。
应该是这样的:
view.setSource (QUrl("qrc:///qml/qmlDeploy/main.qml"));
因为qrc://
是URL前缀,/
是根前缀,而main.qml
是其中的文件
相关文章:
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 找不到QtResource文件中的文件
- "assimp/config.h"找不到,但它在文件夹中
- C++系统找不到指定的文件错误
- 在 Ubuntu 18.04 上,我编译的 cpp 可执行文件找不到 VTK7 依赖项
- Pyinstaller创建的可执行文件找不到Meipass目录中存在的DLL
- 如何修复"QtCharts"库文件找不到/无法包含<QtCharts>?
- 为什么生成文件找不到包含目录
- 我的类在 .h 文件中定义,.cpp文件找不到
- 生成文件找不到 .o 文件
- 部署Qt-dll文件-找不到windows插件,尽管平台/qwindows.dll存在,因此存在libEGL和libGL
- 资源中的文件找不到本地文件
- C++包括 C 文件(找不到 typedef)
- C++生成文件找不到文件
- Xcode 6.0.1版gmp.h头文件找不到,尽管gmp.h和gmpxx.h在local/include中
- 生成文件找不到同一目录中的文件
- 我的生成文件找不到我的头目录
- 包含的文件 <> 找不到 " " 中包含的文件
- 突然间,Ogre头文件找不到(DEVELOPER_DIR随着Xcode更新而改变)
- 尝试在Xcode 5中包含头文件.找不到文件