Home > Blockchain >  PyQT - QTableWidget get header labels after dragged/moved columns
PyQT - QTableWidget get header labels after dragged/moved columns

Time:04-27

I have a QTableWidget which I fill with data from SQL - I enabled to reorder the columns in the View by

self.horizontalHeader().setSectionsMovable(True)

To give the user perfect experience, I created much of code, that the user can decide which column he want to see by two list boxed, one showing the headers already in the table visible, in the other, all headers possible by the SQL Table. The user can switch the columns left an right and make them visible or not. The user can also put the elements in the list up and down to reorder the headers from the column. For the visible column list I have to get a list with all visible headers in the TableWidget - But I only have one problem: When the user drags a Header like "ID" to first position

example

and I want to get all the headers as a list in a for loop,

header = self.horizontalHeaderItem(column).text()

gives "USERNAME" as first column (position 0) and "ID" as last column (position 3). Also

header = self.horizontalHeader().model().headerData(0,Qt.Orientation.Horizontal)

makes no difference..

Has anybody an idea?

CodePudding user response:

you must iterate with logical index

header = self.horizontalHeader()
    for col in range(self.columnCount()):
        print(self.horizontalHeaderItem(header.logicalIndex(col)).text())
  • Related