Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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

    .pro

    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.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # 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 += \
            main.cpp
    
    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
    QML_DESIGNER_IMPORT_PATH = MySingleton
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.addImportPath("qrc:/MySingleton");
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    main.qml

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

    MySingleton.qml

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

    qmldir

    module MySingleton
    singleton MySingleton 1.0 MySingleton.qml
    

  • Moderators

    @GrahamLa said in Singleton woes:

    engine.addImportPath("qrc:/MySingleton");

    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

    @GrahamLa
    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
    Thanks


Log in to reply