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

I created a Qt Widgets application and then added a qml named "main.qml" to it. My files: 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

to untitled9.pro.
I haven't changed the other codes, how did this happen? This is my first question on stackoverflow, I am trying to make my question clear.

+3


source to share


1 answer


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

Sample code:



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

      

+1


source







All Articles