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. how to "place" dynamic objects?
Forum Updated to NodeBB v4.3 + New Features

how to "place" dynamic objects?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 602 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 13 Aug 2023, 17:17 last edited by mzimmers
    #1

    Hi all -

    I'm trying to dynamically create an object to be displayed within a drawer. Here's what I'm trying:

    Window {
        id: window
        Drawer {
            id: drawer
            onOpened: {
                var component;
                component = Qt.createComponent("MyComponent.qml")
                component.createObject(window);
            }
        }
    }
    

    This places my dynamic component in the window, but not in the drawer. I tried substituting "drawer" for "window" in the createObject() call, but that gives me an error:

    Created graphical object was not placed in the graphics scene.
    

    Can someone comment on the preferred way to do this? Thanks...

    EDIT: a Qt bug may be factoring in this example, but I'm going to leave this open for a bit, in case someone sees something I'm doing wrong.

    1 Reply Last reply
    0
    • M mzimmers
      19 Aug 2023, 23:35

      @johngod interesting idea, but this gives a different error:

      Error: Cannot assign QObject* to QQuickItem*
      
      L Offline
      L Offline
      lemons
      wrote on 21 Aug 2023, 06:31 last edited by lemons
      #4

      Within your createObject() method, you have to pass the proper parent.
      If you want to add something to drawer, you have to pass the drawer, not the window.

      component.createObject(drawer); // not: window
      

      Here a minimal example:
      Note: The component will be added over and over again, once you open the drawer. So in the end you will have many of the same components in the drawer.

      Window {
          height: 680
          width: 480
          visible: true
      
          Button {
              anchors.centerIn: parent
              text: "Open Drawer"
              onClicked: drawer.open()
          }
      
          Drawer {
              id: drawer
              width: 300
              height: parent.height
              edge: "LeftEdge"
      
              Column {
                  id: drawerContent
                  anchors.fill: parent
                  anchors.margins: 5
                  spacing: 20
              }
      
              onOpened: {
                  let component = Qt.createComponent("MyComponent.qml")
                  component.createObject(drawerContent)
              }
          }
      
      // MyComponent.qml
      Rectangle {
          width: parent.width
          height: 20
          color: "red"
      }
      
      M 2 Replies Last reply 21 Aug 2023, 13:37
      0
      • J Offline
        J Offline
        johngod
        wrote on 19 Aug 2023, 11:07 last edited by
        #2

        As a workaround does it work, if you change the parent after the object creation ?

        var myObject = component.createObject(window);
        myObject.parent = drawer
        
        M 1 Reply Last reply 19 Aug 2023, 23:35
        0
        • J johngod
          19 Aug 2023, 11:07

          As a workaround does it work, if you change the parent after the object creation ?

          var myObject = component.createObject(window);
          myObject.parent = drawer
          
          M Offline
          M Offline
          mzimmers
          wrote on 19 Aug 2023, 23:35 last edited by
          #3

          @johngod interesting idea, but this gives a different error:

          Error: Cannot assign QObject* to QQuickItem*
          
          L 1 Reply Last reply 21 Aug 2023, 06:31
          0
          • M mzimmers
            19 Aug 2023, 23:35

            @johngod interesting idea, but this gives a different error:

            Error: Cannot assign QObject* to QQuickItem*
            
            L Offline
            L Offline
            lemons
            wrote on 21 Aug 2023, 06:31 last edited by lemons
            #4

            Within your createObject() method, you have to pass the proper parent.
            If you want to add something to drawer, you have to pass the drawer, not the window.

            component.createObject(drawer); // not: window
            

            Here a minimal example:
            Note: The component will be added over and over again, once you open the drawer. So in the end you will have many of the same components in the drawer.

            Window {
                height: 680
                width: 480
                visible: true
            
                Button {
                    anchors.centerIn: parent
                    text: "Open Drawer"
                    onClicked: drawer.open()
                }
            
                Drawer {
                    id: drawer
                    width: 300
                    height: parent.height
                    edge: "LeftEdge"
            
                    Column {
                        id: drawerContent
                        anchors.fill: parent
                        anchors.margins: 5
                        spacing: 20
                    }
            
                    onOpened: {
                        let component = Qt.createComponent("MyComponent.qml")
                        component.createObject(drawerContent)
                    }
                }
            
            // MyComponent.qml
            Rectangle {
                width: parent.width
                height: 20
                color: "red"
            }
            
            M 2 Replies Last reply 21 Aug 2023, 13:37
            0
            • L lemons
              21 Aug 2023, 06:31

              Within your createObject() method, you have to pass the proper parent.
              If you want to add something to drawer, you have to pass the drawer, not the window.

              component.createObject(drawer); // not: window
              

              Here a minimal example:
              Note: The component will be added over and over again, once you open the drawer. So in the end you will have many of the same components in the drawer.

              Window {
                  height: 680
                  width: 480
                  visible: true
              
                  Button {
                      anchors.centerIn: parent
                      text: "Open Drawer"
                      onClicked: drawer.open()
                  }
              
                  Drawer {
                      id: drawer
                      width: 300
                      height: parent.height
                      edge: "LeftEdge"
              
                      Column {
                          id: drawerContent
                          anchors.fill: parent
                          anchors.margins: 5
                          spacing: 20
                      }
              
                      onOpened: {
                          let component = Qt.createComponent("MyComponent.qml")
                          component.createObject(drawerContent)
                      }
                  }
              
              // MyComponent.qml
              Rectangle {
                  width: parent.width
                  height: 20
                  color: "red"
              }
              
              M Offline
              M Offline
              mzimmers
              wrote on 21 Aug 2023, 13:37 last edited by
              #5
              This post is deleted!
              1 Reply Last reply
              0
              • M mzimmers has marked this topic as solved on 21 Aug 2023, 15:07
              • L lemons
                21 Aug 2023, 06:31

                Within your createObject() method, you have to pass the proper parent.
                If you want to add something to drawer, you have to pass the drawer, not the window.

                component.createObject(drawer); // not: window
                

                Here a minimal example:
                Note: The component will be added over and over again, once you open the drawer. So in the end you will have many of the same components in the drawer.

                Window {
                    height: 680
                    width: 480
                    visible: true
                
                    Button {
                        anchors.centerIn: parent
                        text: "Open Drawer"
                        onClicked: drawer.open()
                    }
                
                    Drawer {
                        id: drawer
                        width: 300
                        height: parent.height
                        edge: "LeftEdge"
                
                        Column {
                            id: drawerContent
                            anchors.fill: parent
                            anchors.margins: 5
                            spacing: 20
                        }
                
                        onOpened: {
                            let component = Qt.createComponent("MyComponent.qml")
                            component.createObject(drawerContent)
                        }
                    }
                
                // MyComponent.qml
                Rectangle {
                    width: parent.width
                    height: 20
                    color: "red"
                }
                
                M Offline
                M Offline
                mzimmers
                wrote on 21 Aug 2023, 15:09 last edited by
                #6

                @lemons said in how to "place" dynamic objects?:

                Note: The component will be added over and over again, once you open the drawer. So in the end you will have many of the same components in the drawer.

                So, can something be done about this? There doesn't appear to be a delete analog to Qt.createComponent.

                L 1 Reply Last reply 24 Aug 2023, 05:42
                0
                • M mzimmers
                  21 Aug 2023, 15:09

                  @lemons said in how to "place" dynamic objects?:

                  Note: The component will be added over and over again, once you open the drawer. So in the end you will have many of the same components in the drawer.

                  So, can something be done about this? There doesn't appear to be a delete analog to Qt.createComponent.

                  L Offline
                  L Offline
                  lemons
                  wrote on 24 Aug 2023, 05:42 last edited by
                  #7

                  @mzimmers late answer, but simply don't hook it up to the onOpened() event, as this event gets triggered each time the drawer opens.

                  You could use the Component.onCompleted() event or any other logic, that only calls the function once (if this is what you want to achieve).
                  → Totally depends on the actual use-case

                  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