PDA

View Full Version : Simulate mouse click



Merylin
20th July 2011, 17:56
Hi :),

I'm trying to do an eye mouse controlled.
I want to simulate the click when the eye is closed.

I already have the cursor position. i'm having problems with the mouse click simulation.

I've tried this:

QTestEventList *eventos = new QTestEventList();
eventos->addMouseClick(Qt::LeftButton, 0, QPoint(x, y), -1);
eventos->simulate(this);

and this:

QTest::mouseClick(this, Qt::LeftButton, 0, QPoint(x, y), -1);

and:

QMouseEvent *p = new QMouseEvent(QEvent::MouseButtonPress, QPoint(x, y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QCoreApplication::sendEvent(this, p);
QMouseEvent *r = new QMouseEvent(QEvent::MouseButtonRelease, QPoint(x, y), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QCoreApplication::sendEvent(this, r);

Nothing works. I think it's because of the receiver, it's a QMainWindow. The thing is, I don't know in which button the mouse is over.

Thanks.

high_flyer
20th July 2011, 18:12
This code:

QTestEventList *eventos = new QTestEventList();
eventos->addMouseClick(Qt::LeftButton, 0, QPoint(x, y), -1);
eventos->simulate(this);

is essentially correct, but if it gets called again, your 'eventos' variable will leak memory.
You better use a member instead of a local variable.

Nothing works.
How do you know that?
What should happen if a mouse is clicked in your widget?

Merylin
20th July 2011, 18:21
thanks for the reply,


This code:
is essentially correct, but if it gets called again, your 'eventos' variable will leak memory.
You better use a member instead of a local variable.


Didn't thought about it.


How do you know that?
What should happen if a mouse is clicked in your widget?


There are buttons in the QMainWindow... The button should be clicked, because the mouse is over it when the event is sent.

high_flyer
20th July 2011, 18:32
Are you sure the x,y position is correct?
You can also give the address of the button it self...

Merylin
20th July 2011, 18:49
Yes, i'm sure the position is correct (because the mouse is moving to where i'm looking at the screen). And the buttons get hovered.
I could give the address of the button if i knew which button was.. I could see which button is hovered, but don't know how.

If everything fails, i'll have to see if the cursor coordinates are at the first button, or second, or third, and so on.. It should work but it doesn't look pretty.

But thanks. :)

high_flyer
21st July 2011, 10:28
Yes, i'm sure the position is correct (because the mouse is moving to where i'm looking at the screen).
Yes, but you might be giving coordinates relative to the wrong widget to the event.
Plot the x,y values out, and see what you get.


I could see which button is hovered, but don't know how.
One option is to install an event filter, and catch the enterEvent() for example, and from that the pointer to the button or what ever other widget is under the mouse.

Merylin
21st July 2011, 11:57
One option is to install an event filter, and catch the enterEvent() for example, and from that the pointer to the button or what ever other widget is under the mouse.

Thank you high_flyer!
I installed event filters and know i know which is the button (using the QEvent::HoverEnter type) !
Works great. :D

anjanu
27th August 2011, 18:59
hope this helps:

include the header file "windows.h"
and use mouse_event(5 param);

here's what I did for Right Click:


mouse_event(MOUSEEVENTF_RIGHTDOWN,100, 200, 0,0);
mouse_event(MOUSEEVENTF_RIGHTUP, 100, 200, 0,0);

JandunCN
18th March 2014, 04:27
Although posted a long time ago , it gives me many tips.Thanks all.