Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating (simulating) fake mouse events in Qt

Tags:

c++

qt

qt4

I have a Qt application (server) that receives mouse positions and state ( mouse pressed, released or mouse move) over the local network from another Qt application. I read in the mouse state and position correctly but I am not able to generate fake messages in the server app to simulate mouse move, mouse pressed events.

The server has all the logic in QGraphicsView to handle mouse move etc and everything works as expected when it gets input from a mouse on the server machine.

But once i try to generate fake mouseEvents by reading mouse position and state sent from other app, it doesn't work.

Surprisingly, if I create fake events and pass it to scene as shown below, it generates mouseMoveEvents but I want to do this for QGraphicsView as it has the logic for handling mouse in the server app.

This works :

QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
pressEvent.setScenePos(QPointF(100, 100));
pressEvent.setButton(Qt::LeftButton);
pressEvent.setButtons(Qt::LeftButton);
QApplication::sendEvent(pGraphicsScene, &pressEvent);

This doesnt work :

    QMouseEvent eve( (QEvent::MouseMove), QPoint(100,100), 
        Qt::NoButton,
        Qt::NoButton,
        Qt::NoModifier   );

qApp->sendEvent(this , &eve);

Can anyone help me understand why I cant generate fake events for GraphicsView and how can this be done.

Thanks

like image 988
user948999 Avatar asked Sep 17 '13 12:09

user948999


2 Answers

You have to send the event to the viewport QWidget like so:

qApp->sendEvent(viewport(), &eve);
like image 101
bunjee Avatar answered Sep 19 '22 15:09

bunjee


I think this will work:

QMouseEvent eve( (QEvent::MouseMove), QPoint(100,100), 
    Qt::NoButton,
    Qt::NoButton,
    Qt::NoModifier   );
myGraphicsView->mouseMoveEvent(&eve);
like image 34
Jeremy Friesner Avatar answered Sep 18 '22 15:09

Jeremy Friesner