我有一个启用了扩展选择的表:
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
,我的问题中的代码工作得很好。
在本例中,
table.selectRow(i)
正在处理多重选择:如果在设置选择后调用
setSelectionMode
,我将获得您描述的行为您需要在每个
QTableWidgetItem
上使用setSelected
通过临时设置
MultiSelection
选择模式,选择每一行。相关问题 更多 >
编程相关推荐