【问题标题】:How can i clean up QFormLayout?如何清理 QFormLayout?
【发布时间】:2015-12-31 02:07:19
【问题描述】:

我想动态更改QFormLayout 的内容。我可以使用布局的takeAt() 方法。 但是rowCount() 持有它的数字,每次我重建表单时,它都会返回一个递增的数字......

当布局项干净时,如何使rowCount 返回0

# coding: utf-8
import sys, os

from PySide.QtGui import *

class TestWindow(QWidget):
    def __init__(self):
        super(TestWindow, self).__init__()

        self.formArea = QWidget(self)
        self.formlayout = QFormLayout()
        self.formArea.setLayout(self.formlayout)

        rebuildButton = QPushButton(self)
        rebuildButton.setText('Rebuild')
        rebuildButton.clicked.connect(self.rebuildForm)

        layout = QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.formArea)
        layout.addWidget(rebuildButton)

        self.rebuildForm()

    def rebuildForm(self):
        # clear all items in formlayout
        print('##### start cleaning #####')
        while self.formlayout.count() :
            print('current count = %d / rowCount = %d' % (self.formlayout.count(),
                                                          self.formlayout.rowCount()))
            item = self.formlayout.takeAt(0)
            del(item)

        # build forms
        for i in xrange(3) :
            lineEdit = QLineEdit(self.formArea)
            self.formlayout.addRow('row %d' % i, lineEdit)

if __name__ == '__main__' :
    app = QApplication(sys.argv)
    win = TestWindow()
    win.show()
    app.exec_()

这是它的输出,将调用 rebuildForm 推送 3 次。

##### start cleaning #####
##### start cleaning #####
current count = 6 / rowCount = 3
current count = 5 / rowCount = 3
current count = 4 / rowCount = 3
current count = 3 / rowCount = 3
current count = 2 / rowCount = 3
current count = 1 / rowCount = 3
##### start cleaning #####
current count = 6 / rowCount = 6
current count = 5 / rowCount = 6
current count = 4 / rowCount = 6
current count = 3 / rowCount = 6
current count = 2 / rowCount = 6
current count = 1 / rowCount = 6
##### start cleaning #####
current count = 6 / rowCount = 9
current count = 5 / rowCount = 9
current count = 4 / rowCount = 9
current count = 3 / rowCount = 9
current count = 2 / rowCount = 9
current count = 1 / rowCount = 9

【问题讨论】:

    标签: python layout pyside


    【解决方案1】:

    你不能。这是一些布局的实现细节(QGridLayout 是另一个),logical 行的数量永远不会减少。这就是为什么没有removeRow() 方法的原因。因此,与其尝试删除所有行,不如重新使用已经存在的行(并在必要时添加更多行)。

    您也可以忽略空行,并使用getWidgetPosition 之类的方法,以便始终使用逻辑行,而不是可视行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-14
      • 2019-06-07
      • 2020-04-16
      • 2011-04-21
      • 2017-01-02
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多