Python PyQt 设置滚动区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20041385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python PyQt Setting Scroll Area
提问by Chris Aung
I am trying to make my QGroupBoxscrollable once it grow higher than 400px. The contents in the QGroupBoxare generated using a for loop. This is an example of how it was done.
QGroupBox一旦它增长到 400 像素以上,我就会尝试使我的可滚动。中的内容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)
Since the value of valdepends on some other factors, the myformlayout size could not be determined. In order to solve this, i added a QScrollableArealike this.
由于 的值val取决于其他一些因素,myform因此无法确定布局大小。为了解决这个问题,我添加了一个QScrollableArea这样的。
scroll = QtGui.QScrollableArea()
scroll.setWidget(mygroupbox)
scroll.setWidgetResizable(True)
scroll.setFixedHeight(400)
Unfortunately, that doesn't seems to make any effect on the groupbox. No sign of scrollbar. Am i missing somthing?
不幸的是,这似乎对 groupbox 没有任何影响。没有滚动条的迹象。我错过了什么吗?
采纳答案by ekhumoro
Other than the obvious typo (I'm sure you meant QScrollArea), I can't see anything wrong with what you've posted. So the problem must lie elsewhere in your code: a missing layout maybe?
除了明显的错字(我确定你的意思是QScrollArea),我看不出你发布的内容有什么问题。所以问题一定出在你的代码的其他地方:可能是缺少布局?
Just to make sure we're on the same page, this minimal script works as expected for me:
为了确保我们在同一页面上,这个最小的脚本对我来说按预期工作:
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_())

