I am trying to find out the QGraphicsItem under the mouse (when mouse hover over the item). For that I am using itemAt()
method but itemAt()
is not returning QGraphicsItem.
I tried this way:
bool myViewer::eventFilter(QObject *watched, QEvent *event)
{
bool fEvent = false;
switch(event->type())
{
case QEvent::MouseButtonPress:
{....}
case QEvent::MouseButtonRelease:
{...}
case QEvent::Enter:
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
qDebug()<<"Event points = "<< mouseEvent->pos(); // showing points properly
QPointF po = _view->mapToScene(mouseEvent->pos());
QGraphicsRectItem* rItem = qgraphicsitem_cast<QGraphicsRectItem*>(_scene->itemAt(po,QTransform()));
if(rItem)
{
// some logic
}
}
return true;
default:
break;
}
return fEvent;
I tried with QGraphicsView::itemAt()
also. But still it did not work.
Note: In both way following lines works fine.
QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
qDebug()<<"Event points = "<< mouseEvent->pos();
Where am I wrong?
CodePudding user response:
May be you can try handling QEvent::GraphicsSceneMousePress
And then get the position as said below (pseudo code. Not tetsted.)
//Get the scene event object.
QGraphicsSceneMouseEvent* sceneEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(qevent);
//From the scene event object get the scene position.
QPointF po = sceneEvent->scenePos();
Use that position to find the item.