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

How to pass the 'pressed' mouse event to another object which is created only after press event triggered



  • By setting 'propagateComposedEvents' to true, able to pass the mouse event to already present underneath mouse area. But is there a way that if we click on RectA mouse area then RectB is created from that point under RectA and pass the 'pressed' mouse event to RectB.

    import QtQuick 2.15
    import QtQuick.Controls 2.15
    
    ApplicationWindow{
        id: applWindow
        visible: true
        width: 300
        height: 300
        x:100
        y:100
        
    
        Loader{
            id: loader
        }
    
        Component{
            id: rectBComponent
            Rectangle{
                id:rectB
                color:"green"
                width: 50
                height: 50
                z: -1  // to create rectB below the rectA
    
                MouseArea{
                    id:rectBMouseArea
                    anchors.fill: parent
                    onPressed:console.log("rect B recevied press event") // handler to receive the press event
                }
            }
        }
    
        Rectangle{
            id:rectA
            color:"red"
            width: 50
            height: 50
    
            MouseArea{
                id: rectAMouseArea
                anchors.fill: parent
                propagateComposedEvents: true
                onPressed:{
                    loader.sourceComponent = rectBComponent // create rectB when rectA is pressed
                    if(loader.status == Loader.Ready)
                    {
                        loader.item.x = rectA.x
                        loader.item.y = rectA.y
                        loader.item.width = rectA.width
                        loader.item.height = rectA.height
                        mouse.accepted = false // not accept mouse event and pass it to below object
                    }
                }
            }
        }
    }
    

    Please clarify that can I pass the mouse event to another object which is created after the mouse event is triggered. Thanks in advance.


Log in to reply