qt - Get mouse position in child QGraphicsScene -
in qt5, have main window scene:
mywindow::mywindow(qwidget *parent) : qmainwindow(parent) { view = new qgraphicsview(); scene = new qgraphicsscene(); scene->installeventfilter(this); view->setscene(scene); ... setcentralwidget(view); } view , scene both private members of mywindow. i want know, in mywindow class, mouse position when click on scene. that's why use installeventfilter above. , have tried catch event this:
bool mywindow::eventfilter(qobject *target, qevent *event) { if (target == scene) { if (event->type() == qevent::graphicsscenemousepress) { const qgraphicsscenemouseevent* const me = static_cast<const qgraphicsscenemouseevent*>(event); const qpointf position = me->pos(); cout << position.x() << "," << position.y() << endl; } } return qmainwindow::eventfilter(target, event); } this code not work expected: position prints when click on scene 0,0. clue wrong?
qgraphicsscenemouseevent.pos() returns position in coordinates of item on clicked. scene has no items returns (0,0). if want position in scene coordinates use scenepos().
const qpointf position = me->scenepos();
Comments
Post a Comment