问题
I wanted to have a total of 8 groupbox in the Dialog box. I don't know how to associate the horizontal scrollbar so that I can scroll it down and access all the group box. In the code below I have added only 2 as an example. Any help is appreciated.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(300, 20, 81, 71))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.scrollArea = QtGui.QScrollArea(Dialog)
self.scrollArea.setGeometry(QtCore.QRect(30, 20, 251, 251))
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 249, 249))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.groupBox = QtGui.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox.setGeometry(QtCore.QRect(10, 10, 211, 81))
self.groupBox.setObjectName("groupBox")
self.textEdit = QtGui.QTextEdit(self.groupBox)
self.textEdit.setGeometry(QtCore.QRect(10, 20, 171, 51))
self.textEdit.setObjectName("textEdit")
self.groupBox_2 = QtGui.QGroupBox(self.scrollAreaWidgetContents)
self.groupBox_2.setGeometry(QtCore.QRect(10, 110, 211, 111))
self.groupBox_2.setObjectName("groupBox_2")
self.textEdit_2 = QtGui.QTextEdit(self.groupBox_2)
self.textEdit_2.setGeometry(QtCore.QRect(10, 20, 171, 84))
self.textEdit_2.setObjectName("textEdit_2")
self.verticalScrollBar = QtGui.QScrollBar(self.scrollAreaWidgetContents)
self.verticalScrollBar.setGeometry(QtCore.QRect(230, 0, 16, 241))
self.verticalScrollBar.setOrientation(QtCore.Qt.Vertical)
self.verticalScrollBar.setObjectName("verticalScrollBar")
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox.setTitle(QtGui.QApplication.translate("Dialog", "GroupBox", None, QtGui.QApplication.UnicodeUTF8))
self.groupBox_2.setTitle(QtGui.QApplication.translate("Dialog", "GroupBox", None, QtGui.QApplication.UnicodeUTF8))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
回答1:
As I said in the comments, QScrollArea
doesn't need manual QScrollBar
. It will create when needed. I can't be sure what your problem is without seeing the 'not working' code, but my guess is the fixed sized items and their placement. You are probably putting things outside of the widgets margin or overlapping each other so that the inner widget does not grow appropriately.
Anyway, here is a minimal example that replicates your dialog (notice the scrollbar):
import sys
from PyQt4 import QtGui, QtCore
class MyDialog(QtGui.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
scrolllayout = QtGui.QVBoxLayout()
scrollwidget = QtGui.QWidget()
scrollwidget.setLayout(scrolllayout)
scroll = QtGui.QScrollArea()
scroll.setWidgetResizable(True) # Set to make the inner widget resize with scroll area
scroll.setWidget(scrollwidget)
self.groupboxes = [] # Keep a reference to groupboxes for later use
for i in range(8): # 8 groupboxes with textedit in them
groupbox = QtGui.QGroupBox('%d' % i)
grouplayout = QtGui.QHBoxLayout()
grouptext = QtGui.QTextEdit()
grouplayout.addWidget(grouptext)
groupbox.setLayout(grouplayout)
scrolllayout.addWidget(groupbox)
self.groupboxes.append(groupbox)
self.buttonbox = QtGui.QDialogButtonBox()
self.buttonbox.setOrientation(QtCore.Qt.Vertical)
self.buttonbox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.Ok)
layout = QtGui.QHBoxLayout()
layout.addWidget(scroll)
layout.addWidget(self.buttonbox)
self.setLayout(layout)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog = MyDialog()
dialog.show()
sys.exit(app.exec_())
来源:https://stackoverflow.com/questions/9624281/how-to-associate-a-horizontal-scrollbar-to-multiple-groupbox