Reputation: 83
I am using PyQt table widget with the following settings.
When I use Ctrl to select the first two rows, QTableWidget.selectedItems() returns the following list:
A1, A2, A3, A4, A5, A6, A7, A8, B1, B2, B3, B4, B5, B6, B7, B8
However, if I use Shift to select the first two rows, QTableWidget.selectedItems() returns the following list:
A1, B1, A2, B2, A3, B3, A4, B4, A5, B5, A6, B6, A7, B7, A8, B8
It becomes difficult to predict the order of the list. I see that, capturing the key press event and process the list accordingly is one way. Is there any simpler way to overcome such scenario?
Thanks for the help.
Navin
Upvotes: 2
Views: 934
Reputation: 120608
To get all the selected items in a particular column, you can use the selectedRows method of the table's selection model. But note that this will only work for rows where all columns are selected. Also, for extended selections, the order of the returned indexes may not not always be the same. So to get fully consistent results, the indexes can be sorted first:
indexes = tablewidget.selectionModel().selectedRows(column)
for index in sorted(indexes):
item = tablewidget.itemFromIndex(index)
print(item.text())
Upvotes: 1