5
votes

There is a little thing that I absolutely need for the application I am developping: I have to be able to drag an object onto another one and at least one of them should notice that they are intersecting. So, I the thing is that one of the items must accept the onEntered signal eventhoug the mouse is pressed form outside.

For example:

import QtQuick 1.0

Rectangle{
    id: base
    width: 500
    height: 500
    MouseArea{ //Even without this mousearea I don't get what i want.
        anchors.fill: parent
        //onPressed:{console.log("big")}
    }

    Rectangle{
       id: t
       width: 100
       height: 100
       color: "red"
       MouseArea{
          anchors.fill: parent
          hoverEnabled: true
          onPressed:{console.log("little-press")}
          onEntered:{console.log("little-enter")}
          drag.target: t
       }
     }
}

What I want is to press the mouse button outside the red square, and move it without releasing the button. When the mouse passes over the red rectangle, I want the signal onEntered to be emitted. I don’t understang why it is not emited because onEntered should only care about the mouse beeing inside the mouseArea, not about the buttons.

Any idea of how to do it?

Thank you very much.

1

1 Answers

5
votes

Your code is not working because at the moment one mouse area will be above another, mouse events will be taken by only that mouse area which is at top (untill you have modified the stealing property etc.). Thus, your bottom rectangle's property like hover etc wont work because all those events will be captured by the top rectangle.

Instead try this :

import QtQuick 1.0

Rectangle
{
    id: topParent
    width: 500
    height: 500
    color: "grey"


    Rectangle
    {
        id: redRectangle
        color: "red"
        width:  80
        height: 80

        onXChanged:
        {
            if(   redRectangle.x  > greenRectangle.x - redRectangle.width
                  && redRectangle.x  < greenRectangle.x + greenRectangle.width
                  && redRectangle.y  > greenRectangle.y - redRectangle.height
                  && redRectangle.y  < greenRectangle.y + greenRectangle.height

               )

            {
                console.log ( "Red Over Green" )
            }
        }

        MouseArea
        {
          anchors.fill: parent
          drag.target: redRectangle
          drag.axis: Drag.XandYAxis
          drag.minimumX: 0
          drag.maximumX: topParent.width
          drag.minimumY: 0
          drag.maximumY: topParent.height
          onPressed: redRectangle.z = greenRectangle.z + 1
        }
    }

    Rectangle
    {
        id: greenRectangle
        color: "green"
        width: 180
        height: 180

        onXChanged:
        {
            if(   greenRectangle.x  > redRectangle.x - greenRectangle.width
                  && greenRectangle.x  < redRectangle.x + redRectangle.width
                  && greenRectangle.y  > redRectangle.y - greenRectangle.height
                  && greenRectangle.y  < redRectangle.y + redRectangle.height

               )


            {
                console.log ( "Green Over Red" )
            }
        }

        MouseArea
        {
          anchors.fill: parent
          drag.target: greenRectangle
          drag.axis: Drag.XandYAxis
          drag.minimumX: 0
          drag.maximumX: topParent.width
          drag.minimumY: 0
          drag.maximumY: topParent.height
          onPressed: greenRectangle.z = redRectangle.z + 1
        }
    }
}

Places where I have printed the messages, you can emit signals as well.