9

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

0

3 Answers 3

10

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

qApp->sendEvent(viewport(), &eve);
1
  • 1
    When I'm sending the event to viewport(), the event does not arrive the target-method. But when the receiver is this, it works fine!
    – peter70
    Dec 18, 2018 at 6:20
3

I think this will work:

QMouseEvent eve( (QEvent::MouseMove), QPoint(100,100), 
    Qt::NoButton,
    Qt::NoButton,
    Qt::NoModifier   );
myGraphicsView->mouseMoveEvent(&eve);
2
  • 2
    It will not work as mouseMoveEvent is a protected method.
    – epsilon
    Sep 17, 2013 at 16:40
  • 3
    So subclass QGraphicsView and call it from a method of the subclass. Sep 17, 2013 at 18:12
2

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.