CMake、QT Quick 和 Visual Studio:“qrc:/main.qml:-1 文件未找到”

问题描述 投票:0回答:2

我正在尝试将

QT Quick
(
QML
) 与
Visual Studio 2015
CMake
一起使用。我从一个示例“QT Quick Controls - Gallery”开始,并将其传输到 CMake。它在
QT Creator
(使用 Visual Studio 的编译器)中工作正常,但在 Visual Studio 中则不行(使用
CMake GUI
生成的解决方案):

QQmlApplicationEngine failed to load component
qrc:/main.qml:-1 File not found

我尝试隔离问题并发现了这个精彩的示例:https://github.com/mattfife/QtQuick-with-cmake,它可以在 QT Creator 中工作(经过一些修改),但在视觉工作室。

qml.qrc
文件是这样的:

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

对于这两种环境(QT Creator 和 Visual Studio),我在构建目录中有一个文件

src/qml.qrc.depends
,其内容正是之前的 qml 文件。

main.cpp
中的重要部分:

QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
    return -1;

如果我删除

CMakeLists.txt
文件中的下一行,则可以在 QT Creator 中重现该错误:

qt5_add_resources(qml_QRC src/qml.qrc)

知道如何解决它或尝试什么吗?例如,我不知道如何调试这个

QQmlApplicationEngine
对象。

c++ visual-studio qt cmake qt-quick
2个回答
1
投票

确保您将生成的文件(我假设在您的情况下是 qrc_qml.cpp )添加到您的项目中。在 Visual Studio 中,右键单击 qml.qrc 时,会出现一个“编译”选项(或 Ctrl+F7)。这将从 qml.qrc 生成一个 cpp 文件。它通常在GenerateFiles文件夹中生成。


0
投票

我在 CLion 中的 CMake 中遇到了同样的问题,这对我有用:

engine.load(QUrl::fromLocalFile("main.qml"));

我在这里找到了解决方案:https://forum.qt.io/topic/80001/possible-reasons-for-a-qrc-main-qml-1-file-not-found-error/3

© www.soinside.com 2019 - 2024. All rights reserved.