子类化QGraphicsItem阻止我能够使用itemAt()在QGraphicsScene / View [英] Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View

查看:179
本文介绍了子类化QGraphicsItem阻止我能够使用itemAt()在QGraphicsScene / View的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将QGraphicsItem子类化为我自己的自定义类Hexagon。当我尝试使用 QGraphicsView :: itemAt QGraphicsScene :: itemAt 的函数时,返回任何我的 Hexagon 对象,因为该函数代替查找 QGraphicsItems

如何告诉它寻找 Hexagon 对象?或者我需要改变我的 Hexagon 类吗?或者甚至重新实现 itemAt()



目前,我还子类化 QGraphicsView ,特别是 mousePressedEvent 来获取点击的 Hexagon 对象的一些信息。

  void LatticeView :: mousePressEvent(QMouseEvent * event)
{
Hexagon * hexagon = itemAt - > pos());
...
}

但是当我尝试编译时,以下错误:


从'QGraphicsItem *'到'Hexagon *'的无效转换


我想要的是能够得到点击的 Hexagon 对象,以便我可以访问一些变量在 Hexagon 类中定义,不在 QGraphicsItem 类中隐含。

  Hexagon * hexagon =(Hexagon *)itemAt(event-> pos()); 

但这里有危险,因为 itemAt()可能会返回 NULL 或该项目可能不是 Hexagon 项目。



事实上,你应该使用下面这样的C ++风格:

  Hexagon * hexagon = dynamic_cast& >(itemAt(event-> pos())); 
if(hexagon!= NULL)
{
hexagon-> hexagonMethod();
}

这将要求通过编译器提供运行时类型信息。 / p>

还有一个 QGraphicsItem 函数,名为 type(),它允许您使用 qgraphicsitem_cast()这需要一些额外的工作,包括定义枚举



另外一件事要注意。根据你的场景和项目如何消耗鼠标事件,你可能不会总是看到你的覆盖 mousePressEvent()得到调用,当你期望它,因为鼠标事件可能永远不会得到如果它是由场景中的某些东西消耗的,则返回视图。


I've subclassed QGraphicsItem into my own custom class, Hexagon. When I try to use a function such as QGraphicsView::itemAt, or QGraphicsScene::itemAt, it won't return any of my Hexagon objects because the function instead looks for QGraphicsItems.
How can I tell it to look for Hexagon objects instead? Or do I need to change something in my Hexagon class? Or even re-implement itemAt()?

Currently, I'm also subclassing QGraphicsView, particlarly mousePressedEvent to get some info about the Hexagon object that is clicked on.

void LatticeView::mousePressEvent(QMouseEvent *event)
{
    Hexagon *hexagon = itemAt(event->pos());
    ...
}

But when I try to compile, I get the following error:

invalid conversion from 'QGraphicsItem*' to 'Hexagon*'

What I want is to be able to get the Hexagon object that is clicked on so that I can access some variables I've defined in the Hexagon class that are not implicit in the QGraphicsItem class.

解决方案

To get this to work, you would need to cast the pointer before assigning it to a pointer variable of another type..

Hexagon *hexagon = (Hexagon*)itemAt(event->pos());

But there is danger here since itemAt() may return NULL or the item may not be a Hexagon item.

In fact, you should use the C++ style cast like this:

Hexagon *hexagon = dynamic_cast<Hexagon*>(itemAt(event->pos()));
if (hexagon != NULL)
{
   hexagon->hexagonMethod();
}

This will require Run-time type information to be available through your compiler.

There is also a QGraphicsItem function available called type() which will allow you to use qgraphicsitem_cast() but that requires a little extra work involving defining an enum.

One additional thing to watch out for. Depending on how your scene and items are consuming mouse events, you may not always see your override of mousePressEvent() get called when you expect it to since the mouse event may never get up to the view if it is consumed by something in the scene.

这篇关于子类化QGraphicsItem阻止我能够使用itemAt()在QGraphicsScene / View的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆