QMainWindow 滚动时遇到问题

发布于 2024-10-12 14:39:02 字数 1462 浏览 1 评论 0原文

一个最小的例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苍暮颜 2024-10-19 14:39:02

完成通过...

mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)

scrollWidget = QtGui.QScrollArea()
scrollWidget.setWidget(mainWidget)
scrollWidget.setWidgetResizable(True) # <---------------

self.setCentralWidget(scrollWidget)

Accomplished via...

mainWidget = QtGui.QWidget()
mainWidget.setLayout(layout)

scrollWidget = QtGui.QScrollArea()
scrollWidget.setWidget(mainWidget)
scrollWidget.setWidgetResizable(True) # <---------------

self.setCentralWidget(scrollWidget)
抱着落日 2024-10-19 14:39:02

我建议将表单直接设置为滚动区域的小部件:

scrollWidget = QtGui.QScrollArea()
scrollWidget.setWidget(FormA())

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文