【发布时间】:2023-12-13 22:51:01
【问题描述】:
我在尝试绕过“推荐”的做事方式时遇到了烦人的事情。
所以,我有一堆卡片。我想让它在我发牌时成为整个场景中最后绘制的对象(典型的bring_to_front 功能)。
推荐的方法是添加到对象的 zValue 直到它大于其他对象,但我希望通过明智地使用stackBefore 方法,它模拟重新组织对象添加到场景中的顺序。
当我在有限的集合中洗牌时,这非常有效(获取所选项目的列表,random.shuffle,对于 item do item.stackBefore(next item)),但在冒泡时它肯定不起作用卡到整个场景的顶部。
我考虑将对象的副本添加到场景中,然后删除原始对象,但似乎我应该能够像使用 Python 列表(或 insertAt 或其他东西)那样做 stackAfter .
示例代码:
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 的所有对象的末尾?我不知道