[英]Python PyQt Setting Scroll Area
我试图使我的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)
由于val
的值取决于其他一些因素,因此无法确定myform
布局大小。 为了解决这个问题,我添加了一个像这样的QScrollableArea
。
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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.