Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why does QQmlComponent::create() return null?
Forum Updated to NodeBB v4.3 + New Features

Why does QQmlComponent::create() return null?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.5k 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.
  • Tom assoT Offline
    Tom assoT Offline
    Tom asso
    wrote on last edited by
    #1

    Under what conditions does QQmlComponent::create(QQmlContext *) fail by returning nullptr? The documentation only says " Returns nullptr if creation failed" without further detail. My backend C++ code tries to instantiate a MessageDialog from the following .qml:

    import QtQuick 2.0
    import QtQuick.Dialogs 1.1
    
    MessageDialog {
      id: messageDialog
      title: "My message"
      text: "Fill in this message from C++"
      onAccepted: {
          console.log("Knew you'd see it my way!")
          // Hide the dialog
          visible = false
      }
    
      Component.onCompleted: visible = true
    }
    
    

    And here's a fragment of my backend constructor:

        QQmlApplicationEngine engine;
    
        // Note that resource URL is a local file, not network,
        // no need to wait before calling create()? 
        QQmlComponent *component = 
              new QQmlComponent(&engine, 
                                QStringLiteral("ui-components/MessageDialog.qml"));
    
        // Following call returns nullptr - why???
        QObject *childItem = component->create();
     
    

    Anyone know why? Thanks!

    jsulmJ raven-worxR 2 Replies Last reply
    0
    • Tom assoT Tom asso

      Under what conditions does QQmlComponent::create(QQmlContext *) fail by returning nullptr? The documentation only says " Returns nullptr if creation failed" without further detail. My backend C++ code tries to instantiate a MessageDialog from the following .qml:

      import QtQuick 2.0
      import QtQuick.Dialogs 1.1
      
      MessageDialog {
        id: messageDialog
        title: "My message"
        text: "Fill in this message from C++"
        onAccepted: {
            console.log("Knew you'd see it my way!")
            // Hide the dialog
            visible = false
        }
      
        Component.onCompleted: visible = true
      }
      
      

      And here's a fragment of my backend constructor:

          QQmlApplicationEngine engine;
      
          // Note that resource URL is a local file, not network,
          // no need to wait before calling create()? 
          QQmlComponent *component = 
                new QQmlComponent(&engine, 
                                  QStringLiteral("ui-components/MessageDialog.qml"));
      
          // Following call returns nullptr - why???
          QObject *childItem = component->create();
       
      

      Anyone know why? Thanks!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Tom-asso said in Why does QQmlComponent::create() return null?:

      QStringLiteral("ui-components/MessageDialog.qml"));

      You're using a relative path: are you sure the QML file is actually found?

      Also, before calling create() - did you check https://doc.qt.io/qt-5/qqmlcomponent.html#isError and https://doc.qt.io/qt-5/qqmlcomponent.html#isNull ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Tom assoT 1 Reply Last reply
      2
      • Tom assoT Tom asso

        Under what conditions does QQmlComponent::create(QQmlContext *) fail by returning nullptr? The documentation only says " Returns nullptr if creation failed" without further detail. My backend C++ code tries to instantiate a MessageDialog from the following .qml:

        import QtQuick 2.0
        import QtQuick.Dialogs 1.1
        
        MessageDialog {
          id: messageDialog
          title: "My message"
          text: "Fill in this message from C++"
          onAccepted: {
              console.log("Knew you'd see it my way!")
              // Hide the dialog
              visible = false
          }
        
          Component.onCompleted: visible = true
        }
        
        

        And here's a fragment of my backend constructor:

            QQmlApplicationEngine engine;
        
            // Note that resource URL is a local file, not network,
            // no need to wait before calling create()? 
            QQmlComponent *component = 
                  new QQmlComponent(&engine, 
                                    QStringLiteral("ui-components/MessageDialog.qml"));
        
            // Following call returns nullptr - why???
            QObject *childItem = component->create();
         
        

        Anyone know why? Thanks!

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

        @Tom-asso said in Why does QQmlComponent::create() return null?:

        Anyone know why?

        QQmlComponent::errors()

        --- 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
        3
        • jsulmJ jsulm

          @Tom-asso said in Why does QQmlComponent::create() return null?:

          QStringLiteral("ui-components/MessageDialog.qml"));

          You're using a relative path: are you sure the QML file is actually found?

          Also, before calling create() - did you check https://doc.qt.io/qt-5/qqmlcomponent.html#isError and https://doc.qt.io/qt-5/qqmlcomponent.html#isNull ?

          Tom assoT Offline
          Tom assoT Offline
          Tom asso
          wrote on last edited by
          #4

          @jsulm said in Why does QQmlComponent::create() return null?:

          https://doc.qt.io/qt-5/qqmlcomponent.html#isNull

          It was a relative path problem - thank you!
          My application did not print out any Qt-level messages to stderr when create() failed - it was silent and mysterious. ;-) Is there any way to make Qt more verbose? For example if it had printed out "QML file not found" that would have saved me hours of trouble!

          Thanks
          Tom

          fcarneyF jsulmJ 2 Replies Last reply
          0
          • Tom assoT Tom asso

            @jsulm said in Why does QQmlComponent::create() return null?:

            https://doc.qt.io/qt-5/qqmlcomponent.html#isNull

            It was a relative path problem - thank you!
            My application did not print out any Qt-level messages to stderr when create() failed - it was silent and mysterious. ;-) Is there any way to make Qt more verbose? For example if it had printed out "QML file not found" that would have saved me hours of trouble!

            Thanks
            Tom

            fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            @Tom-asso said in Why does QQmlComponent::create() return null?:

            Is there any way to make Qt more verbose?

            I don't know if there is an automatic way, but you might start using asserts to check for null and other conditions. The asserts will only translate to code when in debug mode, in release they will disappear.

            Q_ASSERT_X(component != nullptr, "createComponentFunction", QQmlComponent::errors()[0].toString()); // might need a lambda to get all errors in list
            or 
            Q_ASSERT(component != nullptr);
            

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            0
            • Tom assoT Tom asso

              @jsulm said in Why does QQmlComponent::create() return null?:

              https://doc.qt.io/qt-5/qqmlcomponent.html#isNull

              It was a relative path problem - thank you!
              My application did not print out any Qt-level messages to stderr when create() failed - it was silent and mysterious. ;-) Is there any way to make Qt more verbose? For example if it had printed out "QML file not found" that would have saved me hours of trouble!

              Thanks
              Tom

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Tom-asso Did you try to call errors() after creating the instance but before calling create?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              Tom assoT 1 Reply Last reply
              0
              • jsulmJ jsulm

                @Tom-asso Did you try to call errors() after creating the instance but before calling create?

                Tom assoT Offline
                Tom assoT Offline
                Tom asso
                wrote on last edited by
                #7

                @jsulm - No, I didn't know about that function - looks like it may be very useful!

                jsulmJ 1 Reply Last reply
                0
                • Tom assoT Tom asso

                  @jsulm - No, I didn't know about that function - looks like it may be very useful!

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Tom-asso It's what @raven-worx suggested above...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    CrouchEndTiger
                    wrote on last edited by
                    #9

                    Just to add to this.

                    I was having QQmlComponent::create() failing too, and it wasn't any problem with the paths.

                    It turned out to be the root type use to instantiate the component was an ApplicationWindow.

                    I'm not sure if this was clashing with my main QApplicationMainWindow class that I had elsewhere..?

                    Anyway changing the root type to something an Item fixed the problem for me.

                    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