Simple automated Mouse Click does not work!



  • Hi,

    I'm trying to create a application where automated mouse click feature is required.
    I'm using QGuiApplication::sendEvent() for this mouse click.
    @QGuiApplication::sendEvent(gui, new QMouseEvent(QEvent::MouseButtonPress, QPointF(50, 175), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
    QGuiApplication::sendEvent(gui, new QMouseEvent(QEvent::MouseButtonRelease, QPointF(50, 175), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));@

    I dnt want to paste the whole code here, which makes the post clumsy. So, here are the links:
    main.cpp code: http://paste.ofcode.org/3bet9UCkASfxDK4VEpfNVRq
    ButtonMouse.h code: http://paste.ofcode.org/34G57qthyS5L2q5NYs9B8Vn
    ButtonMouse.cpp code: http://paste.ofcode.org/kXjrDduqjtEp49jDLTYbwg
    TryMouse.qml code: http://paste.ofcode.org/j4BSiidf7pPaExjfeVmc4k

    The mouse click is not happening as I wanted, Console.log() output clearly shows that.
    In the QML file I made 4 boxes which are click-able. I wanted to test If clicking on Box 1 can also initiate Box 4 Click and vice-verse. So are Box 2 & 3.

    Please guide me how to do this.

    EDIT: Updated code & isssue is below in reply i.e http://qt-project.org/forums/viewthread/34913/#150866 The links above will expire in a week from creation & anyway they are changed as suggested by raven-worx.

    Edit: "Ready to test project is here! ":https://github.com/inblueswithu/InAppMouse/releases/tag/v0.1
    The issue now is "Click through code is not working well. It is getting clicked recursively at the first click position, even though coordinates given are somewhere else"

    Thanks in Advance,
    inblueswithu


  • Moderators

    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.



  • 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!


  • Moderators

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



  • [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!


  • Moderators

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



  • [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!


  • Moderators

    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.



  • 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?



  • 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!! :(


  • Moderators

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



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


  • Moderators

    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??



  • 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



  • [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)


  • Moderators

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



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


  • Moderators

    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.



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



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



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.