PyQt QScrollArea不滚动 [英] PyQt QScrollArea not scrolling
问题描述
我想将UI的某些元素放在滚动区域中,因为其中可能有很多元素.我尝试了以下代码,但是随着我在上面添加了更多元素,该领域一直在增长.
I want to put some elements of my UI in a scroll area as there can be a lot of them. I tried the following peice of code, but the area just keeps growing as I put more elements on it.
在第一部分中,我设置了一个滚动区域,一个小部件和一个布局.我将布局应用于窗口小部件,并将窗口小部件设置为滚动区域.然后,我在外部函数中填写布局.所有按钮下方的按钮允许在布局中填充更多元素.
In the first part I set up a scroll area, a widget and a layout. I apply the layout to the widget and I set the widget to the scrollarea. Then I fill in my layout in an external function. The button under all of it allows to fill more elements in the layout.
scrollRow = QtGui.QScrollArea()
scrollRow.setMaximumSize(600, 400)
self.rowAssetWidget = QtGui.QWidget()
self.rowAssetLayout = QtGui.QGridLayout()
self.rowAssetLayout.setSpacing(20)
self.rowAssetWidget.setLayout(self.rowAssetLayout)
scrollRow.setWidget(self.rowAssetWidget)
#self.mainLayout.addLayout(self.rowAssetLayout, 2, 0)
self.mainLayout.addWidget(self.rowAssetWidget, 2, 0)
self.assetRow()
self.addAssetRowBtn = QtGui.QPushButton("+")
self.addAssetRowBtn.setFixedSize(20, 20)
self.mainLayout.addWidget(self.addAssetRowBtn, 3, 0)
self.connect(self.addAssetRowBtn, QtCore.SIGNAL("clicked()"), self.addAssetRow)
我的元素看起来不错,但是没有滚动.有什么主意吗?
My elements appear fine, but it is not scrolling. Any idea ?
推荐答案
import sys
from PyQt4 import QtGui,QtCore
class LayoutTest(QtGui.QWidget):
def __init__(self):
super(LayoutTest, self).__init__()
self.horizontalLayout = QtGui.QVBoxLayout(self)
self.scrollArea = QtGui.QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtGui.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 280))
self.horizontalLayout_2 = QtGui.QHBoxLayout(self.scrollAreaWidgetContents)
self.gridLayout = QtGui.QGridLayout()
self.horizontalLayout_2.addLayout(self.gridLayout)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.add_button = QtGui.QPushButton("Add Items")
self.horizontalLayout.addWidget(self.scrollArea)
self.horizontalLayout.addWidget(self.add_button)
self.connect(self.add_button, QtCore.SIGNAL("clicked()"), self.addButtons)
self.setGeometry(300, 200, 400, 300)
def addButtons(self):
for i in range(0, 50):
self.r_button = QtGui.QPushButton("Button %s " % i)
self.gridLayout.addWidget(self.r_button)
def run():
app = QtGui.QApplication(sys.argv)
ex = LayoutTest()
ex.show()
sys.exit(app.exec_())
if __name__ == "__main__":
run()
我知道回答这个问题为时已晚,但这是一个有效的示例,您错过了父级布局.
I know its too late to answer for this question, but here is a working example and you missing the parent layout.
这篇关于PyQt QScrollArea不滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!