栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

pyside - how to delete widgets from gridLayout

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/611477.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号