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. Create a child instance of the current object
QtWS25 Last Chance

Create a child instance of the current object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 248 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.
  • Q Offline
    Q Offline
    qcoderpro
    wrote on last edited by
    #1

    Here below

    import QtQuick
    DocumentWindow {
        visible: true
    }
    

    is written: Notice that we do not provide a parent element when creating the new instance using createObject. This way, we create new top level elements. If we had provided the current document as parent to the next, the destruction of the parent window would lead to the destruction of the child windows.

    The question is how to create the new instance to be a child of the current window?

    GrecKoG 1 Reply Last reply
    0
    • Q qcoderpro

      @GrecKo

      I went for testing a couple of ways but ended up not knowing how to provide a parent element there.
      When button new is clicked newDocument function is called:

      function createNewDocument()
        {
            var component = Qt.createComponent("DocumentWindow.qml");
            var window = component.createObject();
            return window;
        }
      
        function newDocument()
        {
            var window = createNewDocument();
            window.show();
        }
      

      Could you guide me.

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #4

      @qcoderpro https://doc.qt.io/qt-6/qml-qtqml-component.html#createObject-method

      1 Reply Last reply
      1
      • Q qcoderpro

        Here below

        import QtQuick
        DocumentWindow {
            visible: true
        }
        

        is written: Notice that we do not provide a parent element when creating the new instance using createObject. This way, we create new top level elements. If we had provided the current document as parent to the next, the destruction of the parent window would lead to the destruction of the child windows.

        The question is how to create the new instance to be a child of the current window?

        GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #2

        @qcoderpro said in Create a child instance of the current object:

        Notice that we do not provide a parent element when creating the new instance using createObject.

        Negating this sentence will do what you want.
        Do provide a parent element when creating the new instance using createObject.

        Q 1 Reply Last reply
        0
        • GrecKoG GrecKo

          @qcoderpro said in Create a child instance of the current object:

          Notice that we do not provide a parent element when creating the new instance using createObject.

          Negating this sentence will do what you want.
          Do provide a parent element when creating the new instance using createObject.

          Q Offline
          Q Offline
          qcoderpro
          wrote on last edited by
          #3

          @GrecKo

          I went for testing a couple of ways but ended up not knowing how to provide a parent element there.
          When button new is clicked newDocument function is called:

          function createNewDocument()
            {
                var component = Qt.createComponent("DocumentWindow.qml");
                var window = component.createObject();
                return window;
            }
          
            function newDocument()
            {
                var window = createNewDocument();
                window.show();
            }
          

          Could you guide me.

          GrecKoG 1 Reply Last reply
          0
          • Q qcoderpro

            @GrecKo

            I went for testing a couple of ways but ended up not knowing how to provide a parent element there.
            When button new is clicked newDocument function is called:

            function createNewDocument()
              {
                  var component = Qt.createComponent("DocumentWindow.qml");
                  var window = component.createObject();
                  return window;
              }
            
              function newDocument()
              {
                  var window = createNewDocument();
                  window.show();
              }
            

            Could you guide me.

            GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #4

            @qcoderpro https://doc.qt.io/qt-6/qml-qtqml-component.html#createObject-method

            1 Reply Last reply
            1
            • Q qcoderpro has marked this topic as solved on

            • Login

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