Sending a custom mouse event to a QGraphicsScene
I am trying to send custom events to an existing QGraphicsScene. the first event is triggered on the stage at the desired location. from now on, all other events go to the same location (as the first event), even if they were provided elsewhere. after I manually clicked somewhere inside the scene, it will "reload" and the next custom event will be received in the right place (all others still go wrong).
my code:
qreal sceneX = customX;
qreal sceneY = customY;
QGraphicsView* view = m_scene->views()[0];
QPoint ptView = view->mapFromScene(sceneX, sceneY);
QPoint ptGlobal = view->viewport()->mapToGlobal(ptView);
QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMouseRelease);
event.setScenePos(QPoint(sceneX, sceneY));
event.setPos(QPoint(sceneX, sceneY));
event.setScreenPos(ptGlobal);
event.setButton(Qt::LeftButton);
event.setButtons(Qt::LeftButton);
event.setModifiers(QApplication::keyboardModifiers());
qApp->sendEvent(m_scene, &event);
customX and customY are just arbitrary x and y that I generate on the fly. I post sample code many times with different customX and customY.
+2
a source to share