Python PyQt设置滚动区域

Chr*_*ung 6 python layout pyqt pyqt4 qscrollarea

QGroupBox一旦它高于400px,我试图使我的可滚动.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)
Run Code Online (Sandbox Code Playgroud)

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

scroll = QtGui.QScrollableArea()
scroll.setWidget(mygroupbox)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
Run Code Online (Sandbox Code Playgroud)

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

ekh*_*oro 17

除了明显的拼写错误(我确定你的意思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_())
Run Code Online (Sandbox Code Playgroud)

  • 学习PyQt5并尝试使用可滚动区域时遇到了这个问题。我意识到`scroll.setWidgetResizable(True)`对于小部件实际出现在滚动区域中至关重要。希望这对以后的任何人有帮助。 (2认同)