我有一个恼人的时间试图绕过'推荐'做某事的方式。QGraphicsItem:为什么没有`stackAfter`方法?
所以,我有一堆卡。我想要这样做,以便在处理卡片时,它成为整个场景中最后绘制的对象(典型的bring_to_front
功能)。
推荐的方法是只添加到对象的zValue
,直到它比所有其他的都大,但我希望取消相当“懒惰”的整数,在整个地方运行,明智地使用stackBefore
方法,该方法模拟重新组织对象添加到场景的顺序。
当我在一个有限的集合中刷洗我的卡片时(获得所选项目的列表,random.shuffle,对于item item.stackBefore(下一个项目)),这个工作方式非常好,但当它涉及到将卡冒泡到整个场景的顶部。
我认为添加对象的副本到场景,然后删除原来的,但它看起来像我应该能够做stackAfter
像使用Python列表(或insertAt
什么的)。
示例代码:
def deal_items(self):
if not self.selection_is_stack():
self.statusBar().showMessage("You must deal from a stack")
return
item_list = self.scene.sorted_selection()
for i,item in enumerate(item_list[::-1]):
width = item.boundingRect().width()
item.moveBy(width+i*width*0.6,0)
another_list = self.scene.items()[::-1]
idx = another_list.index(item)
for another_item in another_list[idx+1:]:
another_item.stackBefore(item)
这工作。这似乎有点......丑陋。
也许有像'setZValue = 0'自动将对象在与zValue == 0所有对象的最终一招?我不知道 – RodericDay 2013-02-26 05:09:23