pyside - how to delete widgets from gridLayout

pyside - how to delete widgets from gridLayout,第1张

pyside - how to delete widgets from gridLayout

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

DesignerMainWindow
class 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)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5617600.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存