Qt将扩展的QGraphicsItem添加到场景中

时间:2012-02-05 20:11:51

标签: c++ qt

我创建了一个类Atom,它扩展了Qt类QGraphicsItem,如下所示:

Atom::Atom(qreal rad, qreal mass, int element, int state) : QGraphicsItem()
{
    // Initialization code
}
void Atom::changeState(int newState)
{
    // Code...
}

然后,我将原子添加到场景中:

Atom *a=new Atom(rad,mass,element,state);
a->setPos(pos);
scene->addItem(a);

但是,Qt将我的Atom类转换为QGraphicsItem类。现在,当我调用scene->items()时,我得到了一个QGraphicsItems的QList,它没有我的Atom类的属性和方法。

所以,我问的问题是:我如何获得我已添加到QGraphicsScene的原子列表?

感谢。

2 个答案:

答案 0 :(得分:2)

您需要将QGraphicsItems转换为Atoms。有关详细信息,请参阅此问题:

Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View

答案 1 :(得分:1)

没有。您的商品未转换为任何商品。它们仍然是您的自定义类型。在C ++中,派生类的所有对象也是它们派生自的类。没有任何东西被转换,所以没有任何东西丢失。

做一个dynamic_cast<Atom*>(item),你会得到你的物品。