Python 如何从QItemSelection收集行
从Python 如何从QItemSelection收集行,python,pyqt,pyside,Python,Pyqt,Pyside,从QItemSelection收集每行QModelIndex列表的最佳方法是什么。当前,QItemSelection为每行和每列返回QModelIndex列表。我只需要为每一行使用它。如果您使用的视图的选择行为是选择行,最简单的方法是: def selected_rows(self, selection): indexes = [] for index in selection.indexes(): if index.column() == 0:
QItemSelection
收集每行QModelIndex
列表的最佳方法是什么。当前,QItemSelection
为每行和每列返回QModelIndex
列表。我只需要为每一行使用它。如果您使用的视图的选择行为是选择行,最简单的方法是:
def selected_rows(self, selection):
indexes = []
for index in selection.indexes():
if index.column() == 0:
indexes.append(index)
return indexes
一个较短(但不是更快)的替代方案是:
from itertools import filterfalse
def selected_rows(self, selection):
return list(filterfalse(QtCore.QModelIndex.column, selection.indexes()))
但是,如果选择行为是选择项目,则需要:
def selected_rows(self, selection):
seen = set()
indexes = []
model = self.tree.model()
for index in selection.indexes():
if index.row() not in seen:
indexes.append(model.index(index.row(), 0))
# or if you don't care about the specific column
# indexes.append(index)
seen.add(index.row())
return indexes
如何创建
QItemSelection
?可通过以下方式访问:selectionChanged(const QItemSelection&selected,const QItemSelection&Diselected),当用户在树视图中进行选择时会触发该选项。QModelIndex与模型的某个项相关联,行和列都不分开。所以我不明白你的意思?