How to determine the new order of rows in QTableWidget after sectionMoved event

431 Views Asked by At

I am creating an application in PyQt5 and have a QTableWidgetItem for which I allow the user to change the order of rows:

tableWidget.verticalHeader().setSectionsMovable(True)

I have connected a onSectionMoved slot to the sectionMoved signal. If I iterate over the contents of the first column in all rows inside onSectionMoved using

for row in range(tableWidget.rowCount()):
    print(tableWidget.item(row, 0).text())

I am still getting the contents of the first column in the old order. How can I get the contents in the new order?

EDIT

To make it more explicit: Assume I have a table

A AA AAA
B BB BBB
C CC CCC

Now, the user moves the first row behind the second one:

B BB BBB
A AA AAA
C CC CCC

My problem is that when iterating inside onSectionMoved I still get A B C as result and not B A C.

1

There are 1 best solutions below

0
On BEST ANSWER

You must translate the row using the logical index:

header = tableWidget.verticalHeader()
for row in range(tableWidget.rowCount()):
    print(tableWidget.item(header.logicalIndex(row), 0).text())