尺寸更改后的GridLayout + ScrollArea小部件位置 [英] GridLayout+ScrollArea widget position after size change

查看:198
本文介绍了尺寸更改后的GridLayout + ScrollArea小部件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有gridlayout的scrollArea,并在其中添加了带有图像的QLabels.当应用程序启动时,它可以正常工作并正确显示标签:

I have a scrollArea with a gridlayout inside it, and i add QLabels to it with images. When the application starts it works fine and displays the labels correctly:

注意:我计算出当前布局空间中可以容纳多少个标签.

Note: i calculate how many labels fit on the current layout space.

如果我最大化它也可以正常工作:

If i maximize it works fine too:

但是当我点击还原时,会发生一些奇怪的事情:

But when i hit restore something weird happens:

您可以看到仅添加了6个标签(与第一个屏幕截图中的标签相同),但是在这里它们都位于彼此重叠的位置.

You can see that only 6 labels are added (the same as in the first Screen shot) but here they are all positioned overlapping each other.

这是ScrollArea和Layout的初始化代码:

This is the initialization code for the ScrollArea and the Layout:

self.scrollArea = QtGui.QScrollArea(self.centralwidget)
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.scrollArea.sizePolicy().hasHeightForWidth())
self.scrollArea.setSizePolicy(sizePolicy)
self.scrollArea.setAutoFillBackground(True)
self.scrollArea.setStyleSheet(_fromUtf8("border: 1px solid blue"))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
self.gridLayoutWidget = QtGui.QWidget()
self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 667, 551))
sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.gridLayoutWidget.sizePolicy().hasHeightForWidth())
self.gridLayoutWidget.setSizePolicy(sizePolicy)
self.gridLayoutWidget.setLayoutDirection(QtCore.Qt.LeftToRight)
self.gridLayoutWidget.setAutoFillBackground(True)
self.gridLayoutWidget.setStyleSheet(_fromUtf8("border: 1px solid red"))
self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
self.scrollArea.setWidget(self.gridLayoutWidget)

推荐答案

因此,感谢freenode上的#pyqt(向Avaris大喊大叫),我现在知道了问题所在.这似乎是QGridLayout中的错误.

So thanks to #pyqt on freenode (shout out to Avaris) i now know what the problem is. It seems to be a bug in QGridLayout.

当我们最大化窗口QGridLayout时,它以12列结尾,当我们执行还原操作时,即使删除了每个项目,布局仍假定为12列,因此在图3中,它显示6张图像,但认为它需要显示12列它只是与其他重叠.

When we maximize the window QGridLayout ends up with 12 columns, when we do a restore even though every item is removed the layout still assumes 12 columns, so in picture 3 it is displaying 6 images but thinks it needs to display 12 so it just overlaps the other ones.

这篇关于尺寸更改后的GridLayout + ScrollArea小部件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆