Qt World Summit: Submit your Presentation

Getting mouse location from animated Rectangle

  • I have a Rectangle with a mouse area that bounces around the screen. It starts when you click the Rectangle. You have to follow the Rectangle with your mouse pressed. But what I notice is that when I press on the Rectangle, the Rectangle moves away (as expected) and it should alert that you are not following anymore since I don't move the mouse from the initial click. but since things are very mouse driven, I tried many things on the Rectangle object to try and detect if the mouse has ever moved from the initital click spot. From the Rectangle object, I don't seem to have any handle on the mouse, even from any of the Rectangle's behavior or transistion events. I tried behavior X on the rectangle as well as XChanged. On both, the rectangle doesn't seem to know the location of the mouse which has clicked awhile ago and remained stationary.

    Any suggestions on how I can get the position of the mouse from an animated object without mouse interaction?

  • Moderators

    @pDogg I'm not sure if I have understood the problem completely. But did you try positionChanged signal. The corresponding handler is onPositionChanged. So for eg:

    Rectangle {
        width: 300
        height: 300
        color: "red"
        MouseArea {
            anchors.fill: parent
            onPositionChanged: console.log(mouseX,mouseY)

  • @p3c0 I tried positionChanged but it's not working as as I need. The problem is all the MouseArea events fire when something happens with the mouse. So I get the first event OnPress. At this point, the user is suppose to follow the moving rectangle. As along as a user follows, all the MouseArea events handle correctly. If the user exits rectangle or releases the mouse, some action happens. All good.
    The problem I have is after the initial onPress, if a user does not release or does not move the mouse, basically, the mouse is at the initial click spot and standing still. Once the rectangle moves away from the still location, an alert should arise that the user is not following along. So no MouseArea event is fired after the initial OnPress. I think onXChanged on the Rectangle might work, but still trying to work it out.

  • What i really need is an OnStop type of event on the mouse. Anytime the mouse is stopped, check if the mouse is still within the MouseArea.

  • Moderators

    @pDogg Does containsMouse fit in your scenario ? But it doesn’t work if the area itself moves while mouse is over it.

  • I tried below. but still not getting what I need. Seems once the mouse stops and stays idle. when the rectangle and rect MA move away, the text value doesn't change to "nope".

    Rectangle {
                onXChanged: {
                    } else {

    oh well, maybe I'll try some javascript to get the cursor.

  • Moderators

    @pDogg As I said earlier it wont work. From the doc for containsMouse:

    Warning: This property is not updated if the area moves under the mouse: containsMouse will not change. In addition, if hoverEnabled is false, containsMouse will only be valid when the mouse is pressed while the mouse cursor is inside the MouseArea.
    I think you will need to calculate it manually.

Log in to reply