Trouble getting QMainWindow to scroll
A minimal example:
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QMainWindow.__init__(self, parent)
winWidth = 683
winHeight = 784
screen = QtGui.QDesktopWidget().availableGeometry()
screenCenterX = (screen.width() - winWidth) / 2
screenCenterY = (screen.height() - winHeight) / 2
self.setGeometry(screenCenterX, screenCenterY, winWidth, win开发者_C百科Height)
layout = QtGui.QVBoxLayout()
layout.addWidget(FormA())
mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)
self.setCentralWidget(mainWidget)
FormA
is a QFrame
with a VBoxLayout that can expand to an arbitrary number of entries.
In the code posted above, if the entries in the forms can't fit in the window then the window itself grows. I'd prefer for the window to become scrollable. I've also tried the following...
replacing
mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)
self.setCentralWidget(mainWidget)
with
mainWidget = QtGui.QScrollArea()
mainWidget.setLayout(layout)
self.setCentralWidget(mainWidget)
results in the forms and entries shrinking if they can't fit in the window.
Replacing it with
mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)
scrollWidget = QtGui.QScrollArea()
scrollWidget.setWidget(mainWidget)
self.setCentralWidget(scrollWidget)
results in the mainwidget (composed of the forms) being scrunched in the top left corner of the window, leaving large blank areas on the right and bottom of it, and still isn't scrollable.
I can't set a limit on the size of the window because I wish for it to be resizable.
How can I make this window scrollable?
Accomplished via...
mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)
scrollWidget = QtGui.QScrollArea()
scrollWidget.setWidget(mainWidget)
scrollWidget.setWidgetResizable(True) # <---------------
self.setCentralWidget(scrollWidget)
I would suggest setting the form directly as the widget of the scroll area:
scrollWidget = QtGui.QScrollArea()
scrollWidget.setWidget(FormA())
self.setCentralWidget(scrollWidget)
If that doesn't work, then take your third example, but set the widget's expansion policy to be expanding or minimum expanding.
精彩评论