2
votes

I'm getting this error:

qrc:/qml/Accessor.qml:5: ReferenceError: MySingleton is not defined

My project structure:

| main.cpp
| main.qml
| qml/
+--- Accessor.qml
+--- MySingleton.qml
+--- qmldir

Contents of the files:

main.cpp:

#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

main.qml:

import QtQuick 2.6
import QtQuick.Window 2.2
import "qml/"

Accessor {
}

qml/Accessor.qml:

import QtQml 2.2
//import qml 1.0

QtObject {
    property int foo: MySingleton.foo
}

qml/MySingleton.qml:

pragma Singleton
import QtQml 2.2

QtObject {
    property int foo: 0
}

qml/qmldir:

module qml

singleton MySingleton 1.0 MySingleton.qml

qml.qrc:

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

Uncommenting the import qml 1.0 in Accessor.qml doesn't help. But when Accessor.qml is in the project root, it works fine.

Any ideas?

1

1 Answers

2
votes

Solution: I had to add import "." into Accessor.qml.