Qt资源中的QML文件找不到文件错误

 诺汐衹湜忲傻 发布于 2023-01-12 13:04

的.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 
#include 

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





    qml/qmlDeploy/main.qml



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

1 个回答
  • QUrl::fromLocalFile ()根本不打算与Qt资源一起使用,因为显然资源包不是本地文件,而且,QML文件不在Qt资源文件的根目录,所以你必须在它之前添加路径:所以只需使用QUrl ("qrc:/qml/qmlDeploy/main.qml")!

    PS:如果你想避免像这样的长路径,只需将.qrc文件移动到与QML文件相同的目录中,你就会有更短的URL.

    2023-01-12 13:07 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有