在给定位置向QTableView对象添加其他行

时间:2016-10-14 23:14:17

标签: python qt pyside

在我的Gui中,我创建了一个带有QStandardItemModel的QTableView,我想在给定位置添加一个额外的行或列。

model.insertRow(idx.row(), QtCore.QModelIndex())
AttributeError: 'PySide.QtGui.QItemSelectionModel' object has no attribute 'insertRow'

不幸的是我收到以下错误:

{{1}}

1 个答案:

答案 0 :(得分:1)

未经测试,但请尝试保留对模型的引用
然后调用模型的适当方法(insertRow,insertColumn) 在视图中,这些方法的效果将很明显。 E.g:

table_view = QtGui.QTableView()
table_view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
table_view.setSelectionBehavior( QtGui.QTableView.SelectItems )
model = QtGui.QStandardItemModel(4, 2)
table_view.setModel(model)
model.insertRow(2, QtCore.QModelIndex())