我有一个表格,已启用ExtendedSelection:
table.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
当我关闭UI时,我使用QSettings来记住任何选择的行。当我重新打开我的UI时,我希望它能自动重新选择这些行。
我有以下代码,但只会选择最后选定的一行:
QSETTINGS = [1, 2, 3] # Indicates row 1, 2 and 3 should be selected
for row in xrange(table.rowCount()):
table_item = table.item(row, 1)
row_data = table_item.data(QtCore.Qt.UserRole)
row_id = row_data
if row_id in QSETTINGS:
table.selectRow(row) # This ends up only making one row selected
如何用除了table.selectRow(row)
之外的方法来选择多个行?
编辑
在我的原问题中,我说我使用的是QtGui.QAbstractItemView.MultiSelection
。然而,我没有使用。我使用的是QtGui.QAbstractItemView.ExtendedSelection
,这也是为什么我的行选择代码显然不起作用的原因。通过暂时切换到MultiSelection
,选择行,然后切换回ExtendedSelection
,我的问题中的代码工作得很好。