python Qt:主控件滚动条

2 投票
2 回答
3943 浏览
提问于 2025-04-15 18:25

我们想在主窗口上加一个滚动条,这样如果用户调整主窗口的大小,滚动条就会出现,用户可以上下移动来查看那些在小窗口外面的子窗口,这样也能左右移动。

下面是带有滚动条的主窗口代码:

def centralWDG(self,MainWindow):
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")

    self.summaryBox = QtGui.QGroupBox("Project Management Layout")
    self.summaryBox.setMinimumHeight(300)
    self.summaryBox.setMinimumWidth(500)

    self.summaryBoxScroll = QtGui.QScrollArea()
    self.summaryBoxScroll.setFrameStyle(QtGui.QFrame.NoFrame)

    self.summaryBoxTopLayout = QtGui.QVBoxLayout(self.summaryBox)
    self.summaryBoxTopLayout.setContentsMargins(1,1,1,1)
    self.summaryBoxTopLayout.addWidget(self.summaryBoxScroll) 

    self.summaryBoxScroll.setWidget(self.centralwidget)

    self.summaryBoxLayout = QtGui.QFormLayout()
    self.summaryBoxLayout.setSpacing(1)
    self.summaryBoxLayout.setSizeConstraint(QtGui.QLayout.SetFixedSize)

    self.summaryBoxLayout = QtGui.QFormLayout(self.centralwidget)
    self.summaryBoxLayout.setSpacing(1)
    self.summaryBoxLayout.setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)

    self.callchildGUIs()

    MainWindow.setCentralWidget(self.centralwidget)

系统启动后,所有的界面都运行得很好,但滚动条却没有显示出来,无论我们把窗口调整得多小都没有。所以,这里缺少了什么呢?

非常感谢大家的评论和建议。

2 个回答

1

我把你给我的一些建议加进去了,谢谢你。

要使用 self.scrollArea.setWidget(self.scrollAreaWidgetContents),首先必须先声明 scrollAreaWidgetContents。下面是更新后的代码——一切都运行得很好:

def centralWDG(self,MainWindow):
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")

    self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
    self.verticalLayout.setObjectName("verticalLayout")

    self.scrollArea = QtGui.QScrollArea()
    self.scrollArea.setWidgetResizable(False)
    self.scrollArea.setObjectName("scrollArea")
    self.scrollArea.setMinimumHeight(400)
    self.scrollArea.setMinimumWidth(400)
    self.scrollArea.setMaximumHeight(1200)
    self.scrollArea.setMaximumWidth(1200)

    self.verticalLayout.addWidget(self.scrollArea)

    self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 1400, 1200))
    self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")

    self.callchildGUIs(self.scrollAreaWidgetContents)

    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    self.verticalLayout_2 = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)
    self.verticalLayout_2.setObjectName("verticalLayout_2")

    MainWindow.setCentralWidget(self.centralwidget)

现在它运行得很好!

3

你使用的 centralWidget(这是一个 QWidget)是主窗口的中心部件,但滚动区域并没有被添加到窗口里。仅仅让它包含中心部件是不够的。

下面的代码是通过 pyuic 生成的:

def setupUi(self, MainWindow):
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")

    self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
    self.verticalLayout.setObjectName("verticalLayout")

    self.scrollArea = QtGui.QScrollArea(self.centralwidget)
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setObjectName("scrollArea")
    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    self.verticalLayout.addWidget(self.scrollArea)

    self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 778, 527))
    self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")

    self.verticalLayout_2 = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)
    self.verticalLayout_2.setObjectName("verticalLayout_2")

    MainWindow.setCentralWidget(self.centralwidget)

滚动区域被添加到了中心部件的布局中,并且有自己的内容部件。如果你把控件添加到 verticalLayout_2(以及 scrollAreaWidgetContents 作为父部件),它们就会有滚动条。

撰写回答