How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?
-
I have a QGraphicsView which contains large number of QGraphicsItem. I want to check, mouse clicked point is inside any of QGraphicsItem or not. And if it is inside in any QGraphicsItem, that item should be highlighted. But though the mouse clicked point is not inside in any QGraphicsItem, some of the polylines are getting highlighted
bool myViewer::eventFilter(QObject* watched, QEvent* event) { bool filterEvent = false; switch (event->type()) { case QEvent::MouseButtonPress: { FindingPosition(event); break; } } } void myViewer::FindingPosition(QEvent* event) { QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); QPointF mousePoint = _view->mapToScene(mouseEvent->pos()); foreach(QGraphicsItem* t , _scene->items()) { if(t->contains(t->mapFromScene(mousePoint))) { QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(t); if (rItem) { rItem->setSelected(false); QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::red); rItem->setPen(mPen); break; } else { QGraphicsPathItem* pItem = qgraphicsitem_cast<QGraphicsPathItem*>(t); if (pItem) { pItem->setSelected(false); QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::red); pItem->setPen(mPen); break; } } } } } } -
I have a QGraphicsView which contains large number of QGraphicsItem. I want to check, mouse clicked point is inside any of QGraphicsItem or not. And if it is inside in any QGraphicsItem, that item should be highlighted. But though the mouse clicked point is not inside in any QGraphicsItem, some of the polylines are getting highlighted
bool myViewer::eventFilter(QObject* watched, QEvent* event) { bool filterEvent = false; switch (event->type()) { case QEvent::MouseButtonPress: { FindingPosition(event); break; } } } void myViewer::FindingPosition(QEvent* event) { QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); QPointF mousePoint = _view->mapToScene(mouseEvent->pos()); foreach(QGraphicsItem* t , _scene->items()) { if(t->contains(t->mapFromScene(mousePoint))) { QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(t); if (rItem) { rItem->setSelected(false); QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::red); rItem->setPen(mPen); break; } else { QGraphicsPathItem* pItem = qgraphicsitem_cast<QGraphicsPathItem*>(t); if (pItem) { pItem->setSelected(false); QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::red); pItem->setPen(mPen); break; } } } } } } -
I have a QGraphicsView which contains large number of QGraphicsItem. I want to check, mouse clicked point is inside any of QGraphicsItem or not. And if it is inside in any QGraphicsItem, that item should be highlighted. But though the mouse clicked point is not inside in any QGraphicsItem, some of the polylines are getting highlighted
bool myViewer::eventFilter(QObject* watched, QEvent* event) { bool filterEvent = false; switch (event->type()) { case QEvent::MouseButtonPress: { FindingPosition(event); break; } } } void myViewer::FindingPosition(QEvent* event) { QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event); QPointF mousePoint = _view->mapToScene(mouseEvent->pos()); foreach(QGraphicsItem* t , _scene->items()) { if(t->contains(t->mapFromScene(mousePoint))) { QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(t); if (rItem) { rItem->setSelected(false); QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::red); rItem->setPen(mPen); break; } else { QGraphicsPathItem* pItem = qgraphicsitem_cast<QGraphicsPathItem*>(t); if (pItem) { pItem->setSelected(false); QPen mPen; mPen.setWidth(1); mPen.setBrush(Qt::red); pItem->setPen(mPen); break; } } } } } }@tushu said in How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?:
foreach(QGraphicsItem* t , _scene->items())
Qt Graphics is designed so that a much faster way of doing this is QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const (or possibly QList<QGraphicsItem *> QGraphicsScene::items(const QPointF &pos, ...)).
I know nothing about your polylines or highlighting.
-
Check the mapping of mouse position to the scene is right or not. You know the exact positions and rects of all items and debug into the code FindingPosition(QEvent* event) and stop at the wrong selection of the item. Manually check the mouse position and item geometry. You will be able to find the issue quickly. Do not look at the code only.
I had a quick look at what I have. I used customized classes to inherit QGraphicsItem and override
protected:
void mousePressEvent( QGraphicsSceneMouseEvent * event ) override;
In this way it may be easier for you to know which one to highlight. -
@tushu said in How to determine mouse clicked point is inside in any QGraphicsItem from scene in Qt?:
foreach(QGraphicsItem* t , _scene->items())
Qt Graphics is designed so that a much faster way of doing this is QList<QGraphicsItem *> QGraphicsView::items(const QPoint &pos) const (or possibly QList<QGraphicsItem *> QGraphicsScene::items(const QPointF &pos, ...)).
I know nothing about your polylines or highlighting.
-
Hi,
Did you make the items selectable ?