python Qt: main widget scroll bar
We are trying to put scrollbar on the main widget, so if the user resizes the main window, the scrollbar appears, and let he move up and down to see child widgets that is outside the smaller window widget, allowing it to move right and left.
Here is the code for the main widget with the scroll-bar..
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)
se开发者_开发百科lf.summaryBoxLayout.setSpacing(1)
self.summaryBoxLayout.setSizeConstraint(QtGui.QLayout.SetMinAndMaxSize)
self.callchildGUIs()
MainWindow.setCentralWidget(self.centralwidget)
The system is started, all GUIs work nicely, but the scroll-bar doesn't show up, it doesn't matter if we resize the windows to very small size. So, what is missing here?
All comments and suggestions are highly appreciated.
You use centralWidget
(which is a QWidget
) as the central widget of the main window, the scroll area is never added to the window. Having it contain the central widget is not enough.
The following code has been generated by 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)
The scroll area is added to the layout of the central widget and has its own content widget. If you add controls to verticalLayout_2
(and scrollAreaWidgetContents
as the parent widget), they will receive scroll bars.
I added some of the suggestions you send me. thanks.
To use self.scrollArea.setWidget(self.scrollAreaWidgetContents), scrollAreaWidgetContents must be declared first. Here is the code updated -- doing everything nicely:
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)
Now it works fine!
精彩评论