QGraphicsScene::itemAt() 无法正常工作

发布于 2025-01-16 12:57:09 字数 1257 浏览 4 评论 0原文

我试图找出鼠标下的 QGraphicsItem(当鼠标悬停在该项目上时)。为此,我使用 itemAt() 方法,但 itemAt() 未返回 QGraphicsItem。
我尝试了这种方式:

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;

         

我也尝试使用 QGraphicsView::itemAt() 。但还是没有成功。
注意:以下这两种方式都可以正常工作。

 QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
 qDebug()<<"Event points = "<< mouseEvent->pos();     
 

我哪里错了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭水袖舞倾城 2025-01-23 12:57:11

也许您可以尝试处理 QEvent::GraphicsSceneMousePress

然后获取如下所述的位置(伪代码。未测试。)

//Get the scene event object.
QGraphicsSceneMouseEvent* sceneEvent = dynamic_cast<QGraphicsSceneMouseEvent*>(qevent);

//From the scene event object get the scene position.
QPointF po = sceneEvent->scenePos();

使用该位置来查找该项目。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文