Important: Please read the Qt Code of Conduct -

Singleton woes

  • Hi
    I am at loss with singletons - I have been following an example from the web that runs as expected. But when I developed my own it does not work.
    I would be grateful if someone could explain why I get
    module "MySingleton" is not installed


    QT += qml quick
    CONFIG += c++11
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Refer to the documentation for the
    # deprecated API to know how to port your code away from it.
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    SOURCES += \
    RESOURCES += qml.qrc
    # Additional import path used to resolve QML modules in Qt Creator's code model
    QML_IMPORT_PATH = MySingleton
    # Additional import path used to resolve QML modules just for Qt Quick Designer
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target


    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        if (engine.rootObjects().isEmpty())
            return -1;
        return app.exec();


    import MySingleton 1.0
    import QtQuick 2.9
    import QtQuick.Window 2.2
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")


    pragma Singleton
    import QtQuick 2.0
    import QtQuick.Window 2.2
    QtObject {
        property color labelColor: "white"


    module MySingleton
    singleton MySingleton 1.0 MySingleton.qml

  • Moderators

    @GrahamLa said in Singleton woes:


    should rather be engine.addImportPath(":/")

    Alternatively (considering your current qrc structure) in your main.qml you can also do: import "MySingleton"

  • @GrahamLa
    Ahhhh - thanks

  • @GrahamLa
    However, in Creator my import statement is still reporting module not found, but the application runs - any ideas?

  • @GrahamLa
    In addition if I move MySingleton outside of the qrc it is no longer found??

  • Moderators

    my advice: simply ignore it.
    QtCreator wont inspect into qrc. But you've set the QML_IMPORT_PATH qmake variable which is responsible for this. This variable should contain the (import-)path - a filesystem path to the folder containing the MySingleton directory.

  • Moderators

    @GrahamLa said in Singleton woes:

    In addition if I move MySingleton outside of the qrc it is no longer found??

    if you move it you have to let the qml engine know where to find it of course.
    I thought this is clear by answering your initial question already where the module couldn't be found?

  • @raven-worx
    Yep - I'm getting the hang of it now

Log in to reply