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

Create Rectangle on Click?



  • Hi,

    I am creating a plotting component, and want to be able to add Vertical Lines (marks) to my plot on a mouse click, sth. like this:

            Rectangle{
                id: recMark
                border.width: 0
                width: 2
                height: parent.height
                y: 0
                z: 5
                color: "red"
                opacity: 0.3
                visible: true
                MouseArea {
                    anchors.fill: parent
                    drag.target: recMark
                    drag.axis: Drag.XAxis
    
                }
            }
    
            MouseArea {
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton
                onPressed: {
                    if(mouse.modifiers& Qt.ShiftModifier) {
                        recMark.x = mouseX-recMark.width/2;
                        recMark.visible=true;
                    }
    

    This is basically what I want, for one Line. How would I need to do it, if I want to create a new Rectangle Item every time I click?


  • Moderators

    Use Qt.createComponent(), for example. Here are the docs: link.



  • @maxwell31 hello,

    1st google suggestion for : "qt create component"

    http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

    or simply

    Row{
     Repeater{
      id:r
       model:0
       YourMark{
       
      }
    }
    }
    //-
    MouseArea{
    ...
    onClicked: r.model+=1
    }
    //-
    

Log in to reply