2012-03-08 54 views
0

我子類的QGraphicsItem兩次: RingCollection是一種容器項目是應該充滿型RingCollectionItem的子項的列表。要做到這一點,有一個填入()方法,即(測試)簡單地生成一個項目,並將其母公司爲當前集合。的Qt增加孩子在QGraphicsScene一個的QGraphicsItem衍生物不顯示

現在,當我添加項目到填入方法裏面的集合,它並沒有在現場露面。 當我直接添加它,它的存在。我很困惑(坦率地說,達到這個最小的測試案例花了我很多時間,因爲我不知道在哪裏尋找問題...) 所以這裏去吧:

代碼在主要(摘錄):

// 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); 

空隙填入()方法:

RingCollectionItem newitem; 
newitem.setParentItem(this); 
newitem.setPos(10.0, 10.0); 

也許任何人都可以看到我錯過了什麼,據我可以告訴基本上都做同樣的事情。我最好的猜測是,通過不知何故沒有做什麼,我期待它,雖然我看到的例子做到這一點......(在我的理解,這一個指向當前實例,請大家指正如果我弄錯了)。

乾杯,路易絲

PS:我測試了一下居然填充被調用,以防萬一。它;)

回答

2

你必須在堆上分配您的項目,使其後的功能仍然存在populate()回報

RingCollectionItem *newitem = new RingCollectionItem(this); 
...