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
Forum Updated to NodeBB v4.3 + New Features

Create a child instance of the current object

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 256 Views 1 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.
  • Q Offline
    Q Offline
    qcoderpro
    wrote on 26 Jul 2023, 08:37 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?

    G 1 Reply Last reply 26 Jul 2023, 09:28
    0
    • Q qcoderpro
      28 Jul 2023, 19:35

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

      G Offline
      G Offline
      GrecKo
      Qt Champions 2018
      wrote on 29 Jul 2023, 08:50 last edited by
      #4

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

      1 Reply Last reply
      1
      • Q qcoderpro
        26 Jul 2023, 08:37

        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?

        G Offline
        G Offline
        GrecKo
        Qt Champions 2018
        wrote on 26 Jul 2023, 09:28 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 28 Jul 2023, 19:35
        0
        • G GrecKo
          26 Jul 2023, 09:28

          @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 28 Jul 2023, 19:35 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.

          G 1 Reply Last reply 29 Jul 2023, 08:50
          0
          • Q qcoderpro
            28 Jul 2023, 19:35

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

            G Offline
            G Offline
            GrecKo
            Qt Champions 2018
            wrote on 29 Jul 2023, 08:50 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 29 Jul 2023, 13:22

            1/4

            26 Jul 2023, 08:37

            • Login

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