Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Simple automated Mouse Click does not work!
Forum Updated to NodeBB v4.3 + New Features

Simple automated Mouse Click does not work!

Scheduled Pinned Locked Moved General and Desktop
21 Posts 3 Posters 8.0k Views 1 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.
  • raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by
    #2

    this won't solve your problem but you shouldn't create events like this.
    You implementation leaks.

    Do this instead:
    @
    QMouseEvent pressEvent(QEvent::MouseButtonPress, QPointF(50, 175), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QGuiApplication::sendEvent(gui, &pressEvent);

    QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPointF(50, 175), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier)
    QGuiApplication::sendEvent(gui, &releaseEvent);
    @
    Also note that that the points provided to the event are in local coordinates of the item you send the event to.

    I'm not very experienced with QML yet so the following might be incorrect:
    if i read your code correct you are sending the event to the root object, but expecting the event in it's children. That's wrong IMHO.

    You should try this:
    @
    QtQuick2ApplicationViewer viewer;
    ...
    QMouseEvent pressEvent(QEvent::MouseButtonPress, QPointF(50, 175), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
    QGuiApplication::sendEvent(&viewer, &pressEvent);
    @
    The QtQuick2ApplicationViewer (QQUickView/QQuickWindow) will take care that the event will be forwarded to the right QML item. I think this should solve your problem then.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • I Offline
      I Offline
      inblueswithu
      wrote on last edited by
      #3

      raven-worx : Thanks for the help! I have changed the code accordingly.
      I have removed ButtonMouse.cpp/.h and placed the click slot inside QtQuick2ApplicationViewer as you suggested

      Here are the codes:
      main.cpp: http://pastebin.com/d4wP7kFv
      QML: http://pastebin.com/QP4cA49g
      Click Slot: http://pastebin.com/VmjVmNJq (inside QtQuick2ApplicationViewer.cpp)

      I dnt know why a mouse click on any button is getting clicked repeatedly even with a single click!
      I have made the cursor to move to proposed location of clicking to confirm if the position we are sending to click is correct or the same button!!
      But every button is getting clicked repeatedly on itself until the stack is full!
      Error as below:

      @TryMouse.qml: <error>
      TryMouse.qml:108: RangeError: Maximum call stack size exceeded@

      If you try to run once you can clearly understand what I mean!

      Thanks!

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #4

        probably because you create an endless loop in your app ;)
        @
        QObject::connect(root.get(), SIGNAL(button1pressed(QVariant , QVariant)), &viewer, SLOT(mouseClick(QVariant , QVariant)));
        @

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • I Offline
          I Offline
          inblueswithu
          wrote on last edited by
          #5

          [quote author="raven-worx" date="1384523931"]probably because you create an endless loop in your app ;) [/quote]
          May be! But there is no loop. As I said I'm not trying to click on the button but the adjacent button to it. To prove, the coordinates of it, I also moved the mouse pointer to desired location. My location is correct but the click is not!! How come this is possible.

          As you said, this loop can only happen through connect only if I click on the same mouse area repeatedly, I'm clearly proving that my position of coordinates are correct by moving moving the mouse out of the button mouse area!

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #6

            [quote author="inblueswithu" date="1384524330"]
            May be! But there is no loop. As I said I'm not trying to click on the button but the adjacent button to it. To prove, the coordinates of it, I also moved the mouse pointer to desired location. My location is correct but the click is not!! How come this is possible.
            [/quote]
            As i said the position you provide to the event is in local coordinates of the object you send the event to. So you need to provide the event the "global QML" coordinates.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • I Offline
              I Offline
              inblueswithu
              wrote on last edited by
              #7

              [quote author="raven-worx" date="1384526129"]You need to provide the event the "global QML" coordinates.[/quote]
              Can you please tell me how can I give my application global coordinates.

              By the way my application is running in full-screen mode (not maximized). So, I think the coordinates wont have any issues here as local & global will be same!

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #8

                local and global is in most cases not the same. Local means relative to the receiver widget. When your widget is fullscreen then the local and global coordinates are the same (but only for the full screen widget, not for it's child widgets).
                I meant you should create your coordinates in "global QML coordinates", thus in local coordinates of your QtQuick2ApplicationViewer (QQUickView/QQuickWindow) widget.

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  inblueswithu
                  wrote on last edited by
                  #9

                  raven-worx: I have just checked by changing as following. Then as you said, my local & global coordinates will change.
                  @//viewer.showFullScreen();
                  viewer.showMaximized();@

                  As my application is running in full-screen, they are same. When I change mouse position using setPos(), mouse is moving to the correct position. So, this means, my perception of clicking position is correct.
                  Anyone tried to run this code?

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    inblueswithu
                    wrote on last edited by
                    #10

                    I've added following lines to test the points I have sent relative to global/window/local etc.,
                    All of them gave same result, as my application is full-screen application. This means coordinates I'm sending to the application are correct irrespective of the window/local/global coordinate positions.

                    @ qDebug() << pressEvent.globalPos().x() << "; " << pressEvent.globalPos().y();
                    qDebug() << pressEvent.windowPos().x() << "; " << pressEvent.windowPos().y();
                    qDebug() << pressEvent.screenPos().x() << "; " << pressEvent.screenPos().y();
                    qDebug() << pressEvent.localPos().x() << "; " << pressEvent.localPos().y();@
                    The code is here for mouseClick() : http://pastebin.com/bgVXg38i

                    Please help me!! I have been struggling for this since 3 weeks!! :(

                    1 Reply Last reply
                    0
                    • raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #11

                      sure they are all the same...
                      @
                      QMouseEvent pressEvent(QEvent::MouseButtonPress, QPointF(x, y),
                      Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
                      QMouseEvent releaseEvent(QEvent::MouseButtonRelease, QPointF(x, y),
                      Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);

                      qDebug() << pressEvent.globalPos().x() << "; " << pressEvent.globalPos().y();
                      qDebug() << pressEvent.windowPos().x() << "; " << pressEvent.windowPos().y();
                      qDebug() << pressEvent.screenPos().x() << "; " << pressEvent.screenPos().y();
                      qDebug() << pressEvent.localPos().x() << "; " << pressEvent.localPos().y();
                      

                      @

                      Just reading this 6 lines of code, how should the event return something useful for windowPos()?! So you have have passed local coordinates - which are relative to "???" - but want the global/window pos?! This can't work.

                      Please provide a ready to start project so i just need to open it in QtCreator and press "run".

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        inblueswithu
                        wrote on last edited by
                        #12

                        I'm sorry. I should have done this ages ago.
                        "Here is the link for code till now.":https://github.com/inblueswithu/InAppMouse/releases/tag/v0.1

                        I guess I have made a whole mess with that global/local coordinates. I didnt get this in this context.

                        1 Reply Last reply
                        0
                        • raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #13

                          ok have tried it.... seems to work like expected.
                          But it still has the endless loop in it. Since you trigger your QML signals for both buttons and listen to the signal to trigger it again....

                          Your MouseArea also covers it's whole parent, thus it doesn't matter where you click it always triggers the signal which then leads to the endless recursion.

                          I guess what you want is 2 MouseArea elements which trigger different signals??

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          0
                          • I Offline
                            I Offline
                            inblueswithu
                            wrote on last edited by
                            #14

                            Ya. I want 2 mouse area signals which signal differently.
                            What I did is when I click a mouse area, it should click the adjacent mouse area too. (The one, where the mouse moves, in my code)
                            My code is moving cursor to that desired location but not clicking in that position. It is clicking repeatedly in the first area only.

                            Please observe console output too when clicked buttons. A console output is given to second column Rectangles also.

                            Note: Rectangles beside first column rects are also buttons

                            1 Reply Last reply
                            0
                            • I Offline
                              I Offline
                              inblueswithu
                              wrote on last edited by
                              #15

                              [quote author="raven-worx" date="1385149213"]seems to work like expected. But it still has the endless loop in it. [/quote]
                              No, It does not work as expected. It is just taking the click ! But not doing a software automated click in correct position, as you have observed.

                              [quote author="raven-worx" date="1385149213"]
                              Your MouseArea also covers it's whole parent, thus it doesn't matter where you click it always triggers the signal which then leads to the endless recursion.[/quote]
                              My MouseArea covers the parent Rectangle, That means it only responds only when clicked inside that button. True, it is having endless recursion because somehow the click is happening inside that button though I have provided coordinates to click somewhere else (the button adjacent to it)

                              1 Reply Last reply
                              0
                              • raven-worxR Offline
                                raven-worxR Offline
                                raven-worx
                                Moderators
                                wrote on last edited by
                                #16

                                no your mouse area is overlapping actually both buttons!
                                @
                                width: parent.width/2
                                height: parent.height/2
                                anchors.centerIn: parent
                                @

                                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                If you have a question please use the forum so others can benefit from the solution in the future

                                1 Reply Last reply
                                0
                                • I Offline
                                  I Offline
                                  inblueswithu
                                  wrote on last edited by
                                  #17

                                  Sorry, I guess you are seeing main.qml. But I'm using TryMouse.qml
                                  @viewer.setMainQmlFile(QStringLiteral("qml/MouseSimulation/TryMouse.qml"));@

                                  Note: I had used main.qml for previous testing.

                                  1 Reply Last reply
                                  0
                                  • raven-worxR Offline
                                    raven-worxR Offline
                                    raven-worx
                                    Moderators
                                    wrote on last edited by
                                    #18

                                    yes you are write i looked at the wrong qml file.

                                    I've debugged into your project. When you apply the following changes it works like intended:
                                    @
                                    //button
                                    Rectangle {
                                    id: button1
                                    width: 100
                                    height: 50
                                    anchors.left: parent.left
                                    anchors.top: parent.top
                                    color: "lightgreen"
                                    border.color: "red"
                                    border.width: 1

                                        Text {
                                            anchors.centerIn: parent
                                            text: "Click Me!"
                                        }
                                    
                                        MouseArea {
                                            anchors.fill: parent
                                            propagateComposedEvents: true
                                            onClicked: {
                                                statusText = "button1 pressed"
                                    
                                                console.log("Mouse button1 press")
                                                
                                                mouse.accepted = contains(Qt.point(mouse.x, mouse.y))
                                                if( mouse.accepted )
                                                    button1pressed(150, 25)
                                            }
                                        }
                                    }
                                    

                                    @
                                    So set the propagateComposedEvents to true and only accept the mouse event when it is really inside the mouse area.

                                    For some reason the first mouse area always stole the mouse events before the second one. I don't know the exactly reason for that though.

                                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                    If you have a question please use the forum so others can benefit from the solution in the future

                                    1 Reply Last reply
                                    0
                                    • I Offline
                                      I Offline
                                      inblueswithu
                                      wrote on last edited by
                                      #19

                                      Thankyou so much. I cant tell you how happy I'm.
                                      You really deserve a beer.
                                      [quote author="raven-worx" date="1385366800"]For some reason the first mouse area always stole the mouse events before the second one. I don't know the exactly reason for that though.[/quote]
                                      Ya. I have been struggling for this ! I'm thinking of filing a bug.
                                      Click is being recognized as you suggested. But hovering (onEntered/onExited) are not working! Hope there has some workaround for this.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        ariacorrente
                                        wrote on last edited by
                                        #20

                                        Have you checked the property
                                        "hoverEnabled":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#hoverEnabled-prop ?

                                        bq. Qt5 docs
                                        By default, mouse events are only handled in response to a button event, or when a button is pressed. Hover enables handling of all mouse events even when no mouse button is pressed.
                                        This property affects the containsMouse property and the onEntered, onExited and onPositionChanged signals.

                                        It gave me similar problems some time ago.

                                        1 Reply Last reply
                                        0
                                        • I Offline
                                          I Offline
                                          inblueswithu
                                          wrote on last edited by
                                          #21

                                          [quote author="ariacorrente" date="1385465841"]Have you checked the property
                                          "hoverEnabled":http://qt-project.org/doc/qt-5.0/qtquick/qml-qtquick2-mousearea.html#hoverEnabled-prop ?[/quote]
                                          Ya. I have that. It works before 1st click but then... It dont.

                                          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