How to program a mouse event?
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:
Code:
{
if(event->button())
{
xPos = event->x();
yPos = event->y();
}
}
main
{
...
while(certain condition)
{
while(!(event->button()))
{
event;
}
mouseMoveEvent(event);
...
}
If I do this, it gives a segmentation fault.
I got 0 experience with implementing a mouseclick, unfortunatly :/
Re: How to program a mouse event?
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:
Quote:
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.
Re: How to program a mouse event?
Quote:
Originally Posted by
prasad_N
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....4_21_02_30.png (sorry that I didn't crop image).
Re: How to program a mouse event?
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.
Re: How to program a mouse event?
What exactly are you trying to do?
React to mouse interaction of the user or pretend being the user?
Cheers,
_