Connect QML signal of arbitrary placed .qml-file to Qt slot

114 views Asked by At

I am trying to connect QML signal to Qt slot. I have read that example and here is code

int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);
    qDebug()<<QUrl::fromLocalFile("main.qml");
    QQuickView view(QUrl::fromLocalFile("main.qml"));//I should replace that line
    QObject *item = view.rootObject();

    MyClass myClass;
    QObject::connect(item, SIGNAL(qmlSignal(QString)),
                     &myClass, SLOT(cppSlot(QString)));

    view.show();
    return app.exec();

}

It works fine, but the problem is that main.qml should be in the folder where .exe file is. So, every time I modify main.qml I should copy it to another foder. File path to main.qml is: "C:\Qt\projects\ConnectionsQT\main.qml". I have tried to replace line by

QQuickView view(QUrl("‪C:\Qt\projects\ConnectionsQT\main.qml");

and by

QQuickView view(QUrl("‪C:\\Qt\\projects\\ConnectionsQT\\main.qml");

but in that cases the program cannot find the main.qml file. What should I do?

1

There are 1 answers

0
Alexander V On

The good practice is making your qml file part of the app executable by putting into .qrc resource file with Qt creator or by hand:

http://qt-project.org/doc/qt-5/qtquick-deployment.html#managing-resource-files-with-the-qt-resource-system

And the you can make use of it with something like:

QQuickView view(QUrl("qrc:///res/qml/main.qml"));