我正在尝试使我的QGroupBox滚动一旦长于400px就可以滚动。使用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)
.

由于世代odicetagcode的值取决于一些其他因素,因此无法确定QGroupBox布局大小。为了解决这个问题,我添加了像这样的生成古代码。

scroll = QtGui.QScrollableArea()
scroll.setWidget(mygroupbox)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
. 不幸的是,这似乎对GroupBox似乎没有任何影响。没有滚动条的迹象。我缺少一些东西吗?

有帮助吗?

解决方案

除明显的拼写错误之外(我确定你的意思是QScrollArea),我看不到你发布的内容的任何错误。所以问题必须在你的代码中的其他地方谎言:可能是缺少的布局?

只是为了确保我们在同一页面上,这个最小脚本适用于我的预期:

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(400)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(scroll)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(25)
    window.setGeometry(500, 300, 300, 400)
    window.show()
    sys.exit(app.exec_())
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top