Python 从QPersistentModelIndex获取QModelIndex
我有一个Python 从QPersistentModelIndex获取QModelIndex,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我有一个QSortFilterProxyModel,我需要选择要删除的行,所以我使用的是QPersistentModelIndexQPersistentModelIndex可以很好地处理多行选择,而其他方法不能删除所有行。我的问题是,我无法使用QPersistentModelIndex映射到Source,它需要QModelIndex。我怎样才能克服这个问题 model = QStandardItemModel() filter = QSortFilterProxyModel()
QSortFilterProxyModel
,我需要选择要删除的行,所以我使用的是QPersistentModelIndex
QPersistentModelIndex
可以很好地处理多行选择,而其他方法不能删除所有行。我的问题是,我无法使用QPersistentModelIndex
映射到Source,它需要QModelIndex
。我怎样才能克服这个问题
model = QStandardItemModel()
filter = QSortFilterProxyModel()
self.filter.setSourceModel(model)
# Set the model/check function
table_view = QTableView()
table_view.setModel(filter)
# Delete Row using
index_list = []
for model_index in table_view.selectionModel().selectedRows():
index = QPersistentModelIndex(model_index)
index_list.append(index)
if index_list:
for index in index_list:
"The error is here, it only accepts `QModelIndex` and refuses `QPersistentModelIndex`"
ix = table_view.model().mapToSource(index.row())
# ix = table_view.model().mapToSource(index)
item = model.itemFromIndex(ix)
如果要将QPersistentModelIndex转换为QModelIndex,只需使用:
for p_index in index_list:
index = QModelIndex(p_index)
ix = table_view.model().mapToSource(index)