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 QML failed to load as module

Singleton QML failed to load as module

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.9k Views
  • 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.
  • N Offline
    N Offline
    nsourl
    wrote on 25 Aug 2021, 10:48 last edited by
    #1

    Hi,

    I am using Qt 5.15.0 and created a singleton QML file and tried to load it as a module using qmldir.

    But unfortunately, I am receiving an error when QQmlApplicationEngine is trying to load the component.

    What I am doing wrong and getting this frustrating output? Any ideas what's happening?

    Below you can find all the information needed to help me out.

    Thanks!

    Application output

    QQmlApplicationEngine failed to load component
    <Unknown File>:12:5: Composite Singleton Type TestMe is not creatable.
    

    TestMe.qml

    pragma Singleton
    import QtQuick 2.0
    
    Item {
        readonly property string value: "value"
    }
    
    

    main.qml

    import QtQuick 2.15
    import QtQuick.Window 2.15
    
    import base 1.0
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        TestMe {
            value: "new value"
        }
    }
    
    

    qmldir

    module base
    singleton TestMe 1.0 TestMe.qml
    

    qmake

    QML_IMPORT_PATH = $$PWD/imports
    

    main.cpp

        QQmlApplicationEngine engine;
        engine.addImportPath("qrc:/imports");
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
        engine.load(url);
    

    Project structure screenshot
    project_structure.png

    K 1 Reply Last reply 25 Aug 2021, 11:01
    0
    • N nsourl
      25 Aug 2021, 11:12

      @KroMignon

      AFAIK only QtObject modules can be singletons not Item.
      Change TestMe.qml from Item to QtObject and it should works as expected.
      cf . https://wiki.qt.io/Qml_Styling

      Thank you for your prompt reply. In the beginning, I had it with QtObject and changed it back now.

      But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.

      K Offline
      K Offline
      KroMignon
      wrote on 25 Aug 2021, 11:26 last edited by KroMignon
      #4

      @nsourl said in Singleton QML failed to load as module:

      But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.

      Sorry, I read too quickly your first post.
      As you have defined TestMe as singleton, you can not create any instance of it, because its already exists!

      Change your main.qml as follow, it should than work:

      import QtQuick 2.15
      import QtQuick.Window 2.15
      
      import base 1.0
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World") + TestMe.value
      
          Component.onCompleted: TestMe.value ="new value"
      }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      N 1 Reply Last reply 25 Aug 2021, 11:32
      3
      • N nsourl
        25 Aug 2021, 10:48

        Hi,

        I am using Qt 5.15.0 and created a singleton QML file and tried to load it as a module using qmldir.

        But unfortunately, I am receiving an error when QQmlApplicationEngine is trying to load the component.

        What I am doing wrong and getting this frustrating output? Any ideas what's happening?

        Below you can find all the information needed to help me out.

        Thanks!

        Application output

        QQmlApplicationEngine failed to load component
        <Unknown File>:12:5: Composite Singleton Type TestMe is not creatable.
        

        TestMe.qml

        pragma Singleton
        import QtQuick 2.0
        
        Item {
            readonly property string value: "value"
        }
        
        

        main.qml

        import QtQuick 2.15
        import QtQuick.Window 2.15
        
        import base 1.0
        
        Window {
            width: 640
            height: 480
            visible: true
            title: qsTr("Hello World")
        
            TestMe {
                value: "new value"
            }
        }
        
        

        qmldir

        module base
        singleton TestMe 1.0 TestMe.qml
        

        qmake

        QML_IMPORT_PATH = $$PWD/imports
        

        main.cpp

            QQmlApplicationEngine engine;
            engine.addImportPath("qrc:/imports");
            const QUrl url(QStringLiteral("qrc:/main.qml"));
            QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                             &app, [url](QObject *obj, const QUrl &objUrl) {
                if (!obj && url == objUrl)
                    QCoreApplication::exit(-1);
            }, Qt::QueuedConnection);
            engine.load(url);
        

        Project structure screenshot
        project_structure.png

        K Offline
        K Offline
        KroMignon
        wrote on 25 Aug 2021, 11:01 last edited by
        #2

        @nsourl said in Singleton QML failed to load as module:

        What I am doing wrong and getting this frustrating output? Any ideas what's happening?

        AFAIK only QtObject modules can be singletons not Item.
        Change TestMe.qml from Item to QtObject and it should works as expected.
        cf . https://wiki.qt.io/Qml_Styling

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        N 1 Reply Last reply 25 Aug 2021, 11:12
        0
        • K KroMignon
          25 Aug 2021, 11:01

          @nsourl said in Singleton QML failed to load as module:

          What I am doing wrong and getting this frustrating output? Any ideas what's happening?

          AFAIK only QtObject modules can be singletons not Item.
          Change TestMe.qml from Item to QtObject and it should works as expected.
          cf . https://wiki.qt.io/Qml_Styling

          N Offline
          N Offline
          nsourl
          wrote on 25 Aug 2021, 11:12 last edited by
          #3

          @KroMignon

          AFAIK only QtObject modules can be singletons not Item.
          Change TestMe.qml from Item to QtObject and it should works as expected.
          cf . https://wiki.qt.io/Qml_Styling

          Thank you for your prompt reply. In the beginning, I had it with QtObject and changed it back now.

          But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.

          K 1 Reply Last reply 25 Aug 2021, 11:26
          0
          • N nsourl
            25 Aug 2021, 11:12

            @KroMignon

            AFAIK only QtObject modules can be singletons not Item.
            Change TestMe.qml from Item to QtObject and it should works as expected.
            cf . https://wiki.qt.io/Qml_Styling

            Thank you for your prompt reply. In the beginning, I had it with QtObject and changed it back now.

            But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.

            K Offline
            K Offline
            KroMignon
            wrote on 25 Aug 2021, 11:26 last edited by KroMignon
            #4

            @nsourl said in Singleton QML failed to load as module:

            But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.

            Sorry, I read too quickly your first post.
            As you have defined TestMe as singleton, you can not create any instance of it, because its already exists!

            Change your main.qml as follow, it should than work:

            import QtQuick 2.15
            import QtQuick.Window 2.15
            
            import base 1.0
            
            Window {
                width: 640
                height: 480
                visible: true
                title: qsTr("Hello World") + TestMe.value
            
                Component.onCompleted: TestMe.value ="new value"
            }
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            N 1 Reply Last reply 25 Aug 2021, 11:32
            3
            • K KroMignon
              25 Aug 2021, 11:26

              @nsourl said in Singleton QML failed to load as module:

              But the same result as before and the error message exactly the same. I performed a complete clean on the project and QML/JS before running application.

              Sorry, I read too quickly your first post.
              As you have defined TestMe as singleton, you can not create any instance of it, because its already exists!

              Change your main.qml as follow, it should than work:

              import QtQuick 2.15
              import QtQuick.Window 2.15
              
              import base 1.0
              
              Window {
                  width: 640
                  height: 480
                  visible: true
                  title: qsTr("Hello World") + TestMe.value
              
                  Component.onCompleted: TestMe.value ="new value"
              }
              
              N Offline
              N Offline
              nsourl
              wrote on 25 Aug 2021, 11:32 last edited by
              #5

              @KroMignon

              Sorry, I read your too quickly your first post.
              As you have defined TestMe as singleton, you can not create any instance of it, because its already exists!

              Change your main.qml as follow, it should than work:

              import QtQuick 2.15
              import QtQuick.Window 2.15
              
              import base 1.0
              
              Window {
                  width: 640
                  height: 480
                  visible: true
                  title: qsTr("Hello World") + TestMe.value
              
                  Component.onCompleted: TestMe.value ="new value"
              }
              

              Oh yeah you are absolutely right, since it's a singleton the instance already exists!

              Of course now it works as expected! :)

              Thank you for your help!

              1 Reply Last reply
              0

              2/5

              25 Aug 2021, 11:01

              • Login

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