The problem here is caused by an implementation detail of QGridLayout.
Whenever items are deleted from a QGridLayout, the number of logical rows
and columns will never decrease, even though the number of visual rows or
colums may do. Because of this, you should always work directly with the items
in the QGridLayout using methods such as getItemPosition and
itemAtPosition.
Below is a re-write of the
DesignerMainWindowclass from the example using
this approach. Obviously, it may need tweaking somewhat to work with your real
application.
class DesignerMainWindow(QtGui.QMainWindow, Ui_fiberModesMainWindow): def __init__(self, parent = None): super(DesignerMainWindow, self).__init__(parent) self.setupUi(self) self.pb_addRow_1.clicked.connect( self.addRow ) def addRow( self ): rows = self.PropertyLayout.rowCount() columns = self.PropertyLayout.columnCount() for column in range(columns): layout = self.PropertyLayout.itemAtPosition(rows - 1, column) if layout is not None: widget = layout.widget() if isinstance(widget, QtGui.QPushButton): widget.setText('Remove %d' % (rows - 1)) widget.clicked.disconnect(self.addRow) widget.clicked.connect(self.removeRow) else: widget.setEnabled(False) widget = QtGui.QLineEdit(self.centralwidget) self.PropertyLayout.addWidget(widget, rows, 1, 1, 1) widget = QtGui.QPushButton(self.centralwidget) widget.setText('Add Row') widget.clicked.connect(self.addRow) self.PropertyLayout.addWidget(widget, rows, columns - 1, 1, 1) def removeRow(self): index = self.PropertyLayout.indexOf(self.sender()) row = self.PropertyLayout.getItemPosition(index)[0] for column in range(self.PropertyLayout.columnCount()): layout = self.PropertyLayout.itemAtPosition(row, column) if layout is not None: layout.widget().deleteLater() self.PropertyLayout.removeItem(layout)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)