擅长:python、mysql、java
<p>通过临时设置<code>MultiSelection</code>选择模式,选择每一行。</p>
<pre><code>QSETTINGS = [1, 2, 3] # Indicates row 1, 2 and 3 should be selected
# Temporarily set MultiSelection
table.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)
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
# Revert MultiSelection to ExtendedSelection
table.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
</code></pre>