我是 PyQt5 的超级新手,并试图设计一个通知面板,其中最新的通知应该放在顶部而不是底部。对于通知,我使用 QLabel 小部件和通知面板,我使用 QScrollArea 垂直布局,垂直策略设置为 Fixed,否则它将标签放置在中心而不是顶部。
所以,问题是,当我想将新标签放在顶部时,它会将我的新标签放在底部。我试图通过创建一个列表来附加每个新通知标签,然后从最高索引到最低索引遍历列表,从而相应地更改每个标签的 y 轴值,从而弄乱几何函数。这是代码:
if len(self.alert_labels_list) > 0:
loop_ran = 0
for l_index in range(len(self.alert_labels_list), 0, -1):
temp_label = self.alert_labels_list[l_index-1]
new_ay = self.label_height + 31 # 31 is the y pos for first label while label height is self defined variable that contains the set height which in this case is 80.
self.label_height = new_ay
temp_label.setGeometry(11, new_ay, 430, 80)
self.x_label.setGeometry(11, 31, 430, 80) # x_label is the new label being added.
self.label_height = 80
[...]
# code which sets the label
[...]
self.alert_labels_list.append(self.x_label)
这里的想法是,从最高索引开始,我们将遇到最新的标签(警报),并通过将(固定的、定义的)标签高度添加到 prev 标签的 y 位置,我们将使所有标签按最近的顺序出现. 我希望这是有道理的。无论如何,上面的这个逻辑仍然不起作用,也许是因为布局不允许我自定义添加到它的新小部件的位置或其他什么?
如果您能指导我如何解决这个问题,我将不胜感激!谢谢!
非常感谢@musicamante,他的评论包含了答案。我只是在这里引用他的话,“ ...如果您使用框布局并想在“开始”插入小部件,请使用 layout.insertWidget(0, widget)
“
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句