Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Add rectangles inside a rectangle from script



  • I would like to add rectangles in a bigger rectangle from the java script.
    I want to do it that way because the size and the number of rectangle can be different at some times. I don't know how to do that can annybody guide me a bit through this ?

    Thank you in advande


  • Lifetime Qt Champion



  • I saw that doc but I feel like it is not the good solution for what I want to do.

    the code I will have will be something like (should do something like this code):

    for (i=0, i<numberOfRectangle, i++){
       myParentRect.addRect (width= parent.width/numberOfRectangle, id= "rect"+i) //The addRect method does not exist and it is what I need to find. 
    }
    

    The doc is more for a one shot creation am I wrong ? Maybe I completly misunderstood it


  • Qt Champions 2018

    If your rectangles should be instantiated based on some data, I would recommend you to use a model/view approach instead of a dynamic creation approach.

    I believe dynamic creation should be reserved to case when an object is temporarily created from an event, like an alert or a dialog.

    In your case I would do something like this:

    RowLayout { // you can change the container if you want, but from your code snippet (width = parent.width/numberOfRectangle), it seems to be the one you need
        // sizing with width/height or anchors
        Repeater {
            model: numberOfRectangle
            Rectangle {
                Layout.fillWidth: true
                Layout.fillHeight: true
            }
        }
    }


  • @GrecKo You are probably right I going to try it this way. Is it possible to implement different actions on each rectangle ? (I want to ad a mouse area in it and add an action to it, but it has to be different for each rectangle)


Log in to reply