QMainWindow 滚动时遇到问题
一个最小的例子:
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, winHeight)
layout = QtGui.QVBoxLayout()
layout.addWidget(FormA())
mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)
self.setCentralWidget(mainWidget)
FormA
是一个带有 VBoxLayout 的 QFrame
,可以扩展到任意数量的条目。
在上面发布的代码中,如果表单中的条目无法容纳在窗口中,则窗口本身会增长。我希望窗口可以滚动。我还尝试了以下操作...
替换
mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)
self.setCentralWidget(mainWidget)
为
mainWidget = QtGui.QScrollArea()
mainWidget.setLayout(layout)
self.setCentralWidget(mainWidget)
表格中的结果,如果它们无法放入窗口,则条目会缩小。
替换它会
mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)
scrollWidget = QtGui.QScrollArea()
scrollWidget.setWidget(mainWidget)
self.setCentralWidget(scrollWidget)
导致主窗口小部件(由表单组成)在窗口的左上角被挤压,在窗口的右侧和底部留下大的空白区域,并且仍然不可滚动。
我无法对窗口的大小设置限制,因为我希望它可以调整大小。
我怎样才能使这个窗口可滚动?
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, winHeight)
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完成通过...
Accomplished via...
我建议将表单直接设置为滚动区域的小部件:
如果这不起作用,则采用第三个示例,但将小部件的扩展策略设置为扩展或最小扩展。
I would suggest setting the form directly as the widget of the scroll area:
If that doesn't work, then take your third example, but set the widget's expansion policy to be expanding or minimum expanding.