I got errors: "QQmlApplicationEngine failed to load component" and "qrc:/main.qml:-1 File not found"

Peter Zhang picture Peter Zhang · Apr 21, 2015 · Viewed 7.5k times · Source

I created a Qt Widgets application, then added a qml named "main.qml" to it. My files are dialog.cpp, dialog.h, dialog.ui, main.cpp, untitiled9.pro, main.qml in qml.qrc

main.cpp:

#include "dialog.h"
#include <QApplication>
#include <QQmlApplicationEngine>
#include<QtQml>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    // w.show();
    QQmlApplicationEngine engine;
    // engine.load(QUrl(QStringLiteral("qrc://main.qml")));
    engine.load(QUrl::fromLocalFile("qrc:///main.qml"));
    return a.exec();
}

I wrote QT += qml quick widgets in untitled9.pro.
I didn't modified other codes, how did this happen? This is my first question in stackoverflow, I try to make my question clear.

Answer

douyw picture douyw · Apr 21, 2015

QUrl::fromLocalFile will build a local file based url. So, just remove "qrc:///" in the code. Copy main.qml into build target directory if necessary.

Sample code:

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