Selection of Graphics Items
-
Hello,
I am struggling to use:
mousePressEvent,
QGraphicsSceneMouseEvent::scenePos() and
QGraphicsSceneMouseEvent::itemAt().In my application I am using a main widget with a graphicsScene and a graphicsView (only one view), by clicking on the scene with the mouse I am drawing a new hexagon or selecting an hexagon, more precisely when clicking in an empty part of the scene it creates a new hexagon (which is automatically selected), if I click on an already existing hexagon it selects the hexagon under the mouse, the problem is that it does not always detect the hexagon under the mouse (sometimes mouse is over an hexagon but it creates a new one over it, sometimes it is in a blank part but it does not create a new hexagon instead it selects another hexagon).
I overrode the mousePressEvent of the customized graphicsScene:
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent) { if (mouseEvent->button() == Qt::LeftButton) { QPointF pt = mouseEvent->scenePos(); QGraphicsItem* pHexagonUnderMouse = itemAt(pt, QTransform()); qDebug() << "\n1 Press" << pt; if (pHexagonUnderMouse == Q_NULLPTR) { qDebug() << "2 Creation " << pt << " " << mouseEvent->scenePos(); clearSelection(); GetMainWindow()->CreateHexagonOnScene(QPoint(pt.x(), pt.y())); } else { qDebug() << "2 Selection " << pt << " " << mouseEvent->scenePos(); clearSelection(); GetMainWindow()->SetSelection(static_cast<Hexagon*>(pHexagonUnderMouse)); } GetMainWindow()->UpdateScene(); } }I am not sure to use correctly the itemAt(pt, QTransform()) to detect if there is a QGraphicsItem under the mouse as I don't know what kind of QTransform to use, I am neither sure how to use scenePos(), maybe I should use it with a function such as mapFromItem .... (I don't understand all those functions so far).
Thank you for your help.
-
Ok just found out that my BBox was not correct (the boundingRect()), so it is ok now it is working.
Thx, bye bye