user948999
user948999

Reputation: 143

Generating (simulating) fake mouse events in Qt

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

Upvotes: 9

Views: 20788

Answers (3)

Jason
Jason

Reputation: 499

Is this in: qApp->sendEvent(this , &eve);, the graphics view?

If so, then you can call mouseMoveEvent() directly as suggested in Jeremy Friesner's answer.

If not, then try:

QApplication::sendEvent(myGraphicsView, &eve);

Upvotes: 2

bunjee
bunjee

Reputation: 116

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

qApp->sendEvent(viewport(), &eve);

Upvotes: 10

Jeremy Friesner
Jeremy Friesner

Reputation: 73041

I think this will work:

QMouseEvent eve( (QEvent::MouseMove), QPoint(100,100), 
    Qt::NoButton,
    Qt::NoButton,
    Qt::NoModifier   );
myGraphicsView->mouseMoveEvent(&eve);

Upvotes: 3

Related Questions