在pyside中即时添加小部件

时间:2013-12-21 12:31:55

标签: python widget pyside

pyside很新,所以也许是一个愚蠢的问题。我想创建一个pyside UI,其中包含可变数量的项目,并且还可以在项目运行时添加项目,并使其更加复杂,还需要一个滚动条以将其全部放在屏幕上!

这就是我现在所拥有的:

import sys

from PySide import QtGui
from PySide import QtCore 
class example(QtGui.QWidget):

    def __init__(self, parent= None):
        super(example, self).__init__()

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)

        self.widget = QtGui.QWidget()

        self.layout = QtGui.QGridLayout(self)

        for i in range(5):
            btn = QtGui.QPushButton("test"+str(i))
            self.layout.addWidget(btn,i,0)
            btn.clicked.connect(self.buttonClicked)
        self.count = i
        self.widget.setLayout(self.layout)


        self.scroll = QtGui.QScrollArea()
        self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.scroll.setWidget(self.widget)

        grid.addWidget(self.scroll,3,0)
        self.setLayout(grid)


    def buttonClicked(self):
        title = QtGui.QLabel('Title'+str(self.count))
        self.layout.addWidget(title,self.count + 1,0)
        self.count += 1
        self.widget.addLayout(self.layout,0)
        self.scroll.addWidget(self.widget,0)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    dialog = example()
    dialog.show()

    sys.exit(app.exec_())

但是不知何故,当通过其中一个按钮添加项目时,布局会搞乱。

有人知道如何解决这个问题吗?

感谢名单!

1 个答案:

答案 0 :(得分:5)

你离我不远。您缺少的关键部分是QScrollArea.setWidgetResizable,这将确保scrollarea自动调整其视口大小以适应内容。

我对您的示例做了一些其他调整,并在适当的地方添加了评论:

class example(QtGui.QWidget):
    def __init__(self, parent= None):
        super(example, self).__init__()

        grid = QtGui.QGridLayout()
        grid.setSpacing(10)

        self.widget = QtGui.QWidget()

        # set the widget as parent of its own layout
        self.layout = QtGui.QGridLayout(self.widget)

        for i in range(5):
            btn = QtGui.QPushButton("test"+str(i))
            self.layout.addWidget(btn,i,0)
            btn.clicked.connect(self.buttonClicked)

        # following lines are redundant
        # self.count = i
        # self.widget.setLayout(self.layout)

        self.scroll = QtGui.QScrollArea()
        # need this so that scrollarea handles resizing
        self.scroll.setWidgetResizable(True)
        # these two lines may not be needed now
        self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)

        self.scroll.setWidget(self.widget)

        grid.addWidget(self.scroll, 3, 0)
        self.setLayout(grid)

    def buttonClicked(self):
        title = QtGui.QLabel('Title' + str(self.layout.count()))
        self.layout.addWidget(title)
        # following lines are redundant
        # self.layout.addWidget(title, self.count + 1, 0)
        # self.count += 1
        # self.widget.addLayout(self.layout,0)
        # self.scroll.addWidget(self.widget,0)