python Qt:主窗口小部件滚动条

时间:2010-01-25 05:49:49

标签: python qt scrollbar widget

我们正在尝试将滚动条放在主窗口小部件上,因此如果用户调整主窗口的大小,则会显示滚动条,让他上下移动以查看小窗口窗口小部件之外的子窗口小部件,允许它移动左右。

以下是带有滚动条的主要小部件的代码..

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)

系统启动,所有GUI都运行良好,但滚动条没有显示,如果我们将窗口调整到非常小的尺寸也没关系。那么,这里缺少什么?

所有评论和建议都非常感谢。

2 个答案:

答案 0 :(得分:3)

您使用centralWidgetQWidget)作为主窗口的中央窗口小部件,滚动区域永远不会添加到窗口中。让它包含中央小部件是不够的。

以下代码由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作为父窗口小部件),它们将会收到滚动条。

答案 1 :(得分: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)

现在它运作正常!