13

一旦它长到 400 像素以上,我就试图让我的QGroupBox滚动。中的内容QGroupBox是使用 for 循环生成的。这是如何完成的一个例子。

mygroupbox = QtGui.QGroupBox('this is my groupbox')
myform = QtGui.QFormLayout()
labellist = []
combolist = []
for i in range(val):
    labellist.append(QtGui.QLabel('mylabel'))
    combolist.append(QtGui.QComboBox())
    myform.addRow(labellist[i],combolist[i])
mygroupbox.setLayout(myform)

由于 的值val取决于其他一些因素,myform因此无法确定布局大小。为了解决这个问题,我添加了一个QScrollableArea这样的。

scroll = QtGui.QScrollableArea()
scroll.setWidget(mygroupbox)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)

不幸的是,这似乎对 groupbox 没有任何影响。没有滚动条的迹象。我错过了什么吗?

4

1 回答 1

23

除了明显的错字(我确定您的意思是QScrollArea)之外,我看不出您发布的内容有任何问题。所以问题一定出在代码的其他地方:可能是缺少布局?为了确保我们在同一页面上,下面的最小脚本按我的预期工作:

截屏

PyQt5

from PyQt5 import QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self, val):
        super().__init__()
        mygroupbox = QtWidgets.QGroupBox('this is my groupbox')
        myform = QtWidgets.QFormLayout()
        labellist = []
        combolist = []
        for i in range(val):
            labellist.append(QtWidgets.QLabel('mylabel'))
            combolist.append(QtWidgets.QComboBox())
            myform.addRow(labellist[i],combolist[i])
        mygroupbox.setLayout(myform)
        scroll = QtWidgets.QScrollArea()
        scroll.setWidget(mygroupbox)
        scroll.setWidgetResizable(True)
        scroll.setFixedHeight(200)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(scroll)

if __name__ == '__main__':

    app = QtWidgets.QApplication(['Test'])
    window = Window(12)
    window.setGeometry(500, 300, 300, 200)
    window.show()
    app.exec_()

PyQt4

from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self, val):
        QtGui.QWidget.__init__(self)
        mygroupbox = QtGui.QGroupBox('this is my groupbox')
        myform = QtGui.QFormLayout()
        labellist = []
        combolist = []
        for i in range(val):
            labellist.append(QtGui.QLabel('mylabel'))
            combolist.append(QtGui.QComboBox())
            myform.addRow(labellist[i],combolist[i])
        mygroupbox.setLayout(myform)
        scroll = QtGui.QScrollArea()
        scroll.setWidget(mygroupbox)
        scroll.setWidgetResizable(True)
        scroll.setFixedHeight(200)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(scroll)

if __name__ == '__main__':

    app = QtGui.QApplication(['Test'])
    window = Window(12)
    window.setGeometry(500, 300, 300, 200)
    window.show()
    app.exec_()
于 2013-11-18T19:06:47.300 回答