【发布时间】:2012-03-08 19:30:24
【问题描述】:
我将 QGraphicsItem 子类化了两次: RingCollection 是一种容器项,应该用 RingCollectionItem 类型的子项列表填充。为此,有一个 populate() 方法,它(用于测试)简单地生成一个项目并将其父项设置为当前集合。
现在,当我在 populate 方法中将项目添加到集合中时,它不会出现在场景中。 当我直接添加它时,它就在那里。我很困惑(坦率地说,达到这个最小的测试用例花了我很长时间,因为我不知道在哪里寻找问题......) 就这样吧:
主代码(摘录):
// works, both collection and item are drawn
RingCollection collection(QPoint(0,0),150);
RingCollectionItem newitem;
newitem.setParentItem(&collection);
newitem.setPos(10.0, 10.0);
scene.addItem(&collection);
另一种方法:
//does not work: only the collection is drawn
RingCollection collection(QPoint(0,0),150);
collection.populate();
scene.addItem(&collection);
和 void populate() 方法:
RingCollectionItem newitem;
newitem.setParentItem(this);
newitem.setPos(10.0, 10.0);
也许任何人都可以看到我缺少的东西,据我所知,两者的作用基本相同。我最好的猜测是传递 this 不知何故并没有达到我的预期,尽管我已经看到例子这样做了......(在我的理解中,this 是指向当前实例的指针,如果我弄错了,请纠正我)。
干杯,露易丝
PS:我测试了 populate 实际上会被调用,以防万一。确实如此;)
【问题讨论】: