Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Singleton woes
Forum Updated to NodeBB v4.3 + New Features

Singleton woes

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 2 Posters 1.0k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrahamLa
    wrote on last edited by
    #1

    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
    
    raven-worxR G 2 Replies Last reply
    0
    • G GrahamLa

      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
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @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"

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • G GrahamLa

        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
        
        G Offline
        G Offline
        GrahamLa
        wrote on last edited by
        #3

        @GrahamLa
        Ahhhh - thanks

        G 1 Reply Last reply
        0
        • G GrahamLa

          @GrahamLa
          Ahhhh - thanks

          G Offline
          G Offline
          GrahamLa
          wrote on last edited by
          #4

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

          G raven-worxR 2 Replies Last reply
          0
          • G GrahamLa

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

            G Offline
            G Offline
            GrahamLa
            wrote on last edited by
            #5

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

            raven-worxR 1 Reply Last reply
            0
            • G GrahamLa

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

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @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.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              2
              • G GrahamLa

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

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by raven-worx
                #7

                @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?

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                G 1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @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?

                  G Offline
                  G Offline
                  GrahamLa
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved