I have a ScrollArea, containing a VBoxLayout containing several Labels:
realmScroll = QScrollArea(self.container.widget())
realmScroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
realmScroll.setWidgetResizable(True) # setting this to False has no effect
self.realmLayout = QVBoxLayout(realmScroll)
realmScroll.setWidget(self.realmLayout.widget())
self.container.addWidget(realmScroll)
for i in range(1, 开发者_运维知识库20):
label = QLabel("test #" + str(i))
label.setMinimumHeight(20)
self.realmLayout.addWidget(label)
However when viewed the layout doesn't scroll, it de-stretches (shrinks?) the items together vertically:
I've tried using minimum heights but this doesn't seem to work - what can I do this to make each label appear separately and cause the scrollview to scroll?
Create a separate widget as a container of the labels (below as "labelsContainer") and set it as the scroll area's widget. Make a vertical box layout for the container widget (below as "labelsLayout") and add the labels to that layout.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Test(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.realmScroll = QScrollArea(self)
self.setCentralWidget(self.realmScroll)
self.realmScroll.setWidgetResizable(True)
labelsContainer = QWidget()
self.realmScroll.setWidget(labelsContainer)
labelsLayout = QVBoxLayout(labelsContainer)
for i in range(1, 20):
label = QLabel("test #" + str(i))
labelsLayout.addWidget(label)
app = QApplication(sys.argv)
test = Test()
test.show()
app.exec_()
精彩评论