PyQt QScrollArea not scrolling

允我心安 提交于 2019-12-02 03:04:55
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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!