PDA

View Full Version : How to program a mouse event?



dreak
24th June 2015, 18:59
mouseEvent
Hi, I need to program pool.

Right now, I'd like to make a mouseEvent so that, when i click on the screen, i get the position of the mouse.
I know to use event->x(); to get the xCoordinate, but I don't know how to impletement the clickEvent itself.

I got:



void mouseMoveEvent(QMouseEvent* event)
{
if(event->button())
{
xPos = event->x();
yPos = event->y();
}
}

main
{
...
while(certain condition)
{
QMouseEvent *event;
while(!(event->button()))
{
event;
}

mouseMoveEvent(event);
...
}


If I do this, it gives a segmentation fault.

I got 0 experience with implementing a mouseclick, unfortunatly :/

prasad_N
24th June 2015, 19:34
mouseMoveEvent, mousePressEvent, mouseReleaseEvent's are called automatically when you click on associated widget.

If you want to create customized mouse press event and post it:


QMouseEvent* mousePressEvent= new QMouseEvent(QEvent::MouseButtonPress, QCursor:: pos(), this->mapToGlobal( QCursor:: pos()), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QCoreApplication:: postEvent(this, mousePressEvent);

Note: postEvent() is an asynchronous call while sendEvent() is synchronous call.

dreak
24th June 2015, 20:03
mouseMoveEvent, mousePressEvent, mouseReleaseEvent's are called automatically when you click on associated widget.

If you want to create customized mouse press event and post it:



Note: postEvent() is an asynchronous call while sendEvent() is synchronous call.
Oh yes, this is (almost) what I need!

Right now it gives the global position on my screen, how do I made it that it takes the relative position in a certain part of my 'QGraphicsScene scene' ?

(So only in the green part of the pooltable: http://imagehosting.jumbee.net/file.php?file=e78_screenshot_from_2015_06_24_21_02 _30.png (sorry that I didn't crop image).

prasad_N
24th June 2015, 20:51
Replace Qcursor::pos() with your widget position and maptoGlobal(thatPosition) as next parameter.

in your case :
Qpoint point =yourWidget->geometry().center();
Qpoint glbPos = yourWidget->maptoGlobal(point);

sorry I could not type well as I'm replying from my phone.

anda_skoa
24th June 2015, 21:39
What exactly are you trying to do?

React to mouse interaction of the user or pretend being the user?

Cheers,
_