Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Getting mouse location from animated Rectangle
Forum Updated to NodeBB v4.3 + New Features

Getting mouse location from animated Rectangle

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 2 Posters 2.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pDogg
    wrote on last edited by
    #1

    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?

    p3c0P 1 Reply Last reply
    0
    • P pDogg

      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?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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)
          }
      }
      

      157

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pDogg
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pDogg
          wrote on last edited by
          #4

          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.

          p3c0P 1 Reply Last reply
          0
          • P pDogg

            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.

            p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

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

            157

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pDogg
              wrote on last edited by
              #6

              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: {
                              if(Rect_ma.containsMouse){
                                  text1.text="contains";
                              } else {
                                  text1.text="nope";
                              }
              

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

              p3c0P 1 Reply Last reply
              0
              • P pDogg

                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: {
                                if(Rect_ma.containsMouse){
                                    text1.text="contains";
                                } else {
                                    text1.text="nope";
                                }
                

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

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @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.

                157

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved