从QModelIndex
每行收集QItemSelection
列表的最佳方式是什么?目前QItemSelection
会为每行和每列返回QModelIndex
的列表。我每行只需要它。
答案 0 :(得分:1)
如果您使用的选择行为是选择行的视图,最简单的方法是:
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