【发布时间】:2016-10-13 16:02:13
【问题描述】:
我正在为我的 qtableview 使用 QStandardItemModel。
import ui_my_viewlogs
import os
from PyQt4 import QtCore, QtGui
class my_viewlogs(QtGui.QDialog, ui_my_viewlogs.Ui_viewlogs):
def __init__(self):
super(my_viewlogs, self).__init__()
self.setupUi(self)
self.model = QtGui.QStandardItemModel()
self.tableView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.header_names = ['abc', 'def', 'ghi', 'kjl', 'mno', 'pqr']
self.model.setHorizontalHeaderLabels(self.header_names)
self.tableView.verticalHeader().setVisible(False)
self.tableView.setShowGrid(False)
self.selectionModel = self.tableView.selectionModel()
self.tableView.customContextMenuRequested.connect(self.open_menu)
self.tableView.setModel(self.model)
self.tableView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
def open_menu(self, position):
menu = QtGui.QMenu()
remove_selected_item_icon = QtGui.QIcon()
remove_selected_item_icon.addPixmap(QtGui.QPixmap(":/images /Images/deleteSelected.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
remove_selected_item = menu.addAction(remove_selected_item_icon, "Remove selected item(s) ")
if action == remove_selected_item:
model = self.model
indices = self.tableView.selectionModel().selectedRows()
for index in sorted(indices):
model.removeRow(index.row(), QtCore.QModelIndex())
当我尝试删除所选行时(即 model.removeRow() )我收到错误“TypeError: argument 1 of QAbstractItemModel.removeRow() has an invalid type”。
我已经搜索了很多在 pyqt 的 qtableview 中删除选定行的正确方法。但是,我无法删除选定的行。
您能否分享一个在 pyqt 的 qtableview 中删除选定行的示例代码?
【问题讨论】:
-
为什么要将
QtCore.QModelIndex类传递给removeRow?如果有的话,它应该是一个实例,即。QtCore.QModelIndex(),但这是默认设置,因此最好将其全部删除。 -
@Tim 即使我使用 QtCore.QModelIndex() 或将其全部删除...我仍然遇到相同的错误 ....
-
@tim 感谢您纠正我的拼写错误。
标签: python qt pyqt pyqt4 qtableview