'PyQt5 replace column order QTableView

is it possible to replace the order of columns in a QTableView? Like first it's "name","age","adres", but I want it to change to "age", "name", "adres"?

Is this possible in PyQt5?

i = 0
            
            with open(f"{self.sender().file}", "r") as fileInput:
                
                for row in csv.reader(fileInput):
                    i+=1
                    if i == 1:
                        items = [
                            str(field)
                            for field in row
                        ]
                        CSVmodel.setHorizontalHeaderLabels(items)
                    else:
                        items = [
                            QtGui.QStandardItem(field)
                            for field in row
                        ]
                        CSVmodel.appendRow(items)

code right now.



Solution 1:[1]

You just have to change the indexes:

ORDER = [1, 0, 2]

filename = f"{self.sender().file}"

with open(filename, "r") as f:
    for i, row in enumerate(csv.reader(f)):
        if i == 0:
            items = [str(field) for field in row]
            ordered_items = [items[i] for i in ORDER]
            CSVmodel.setHorizontalHeaderLabels(ordered_items)
        else:
            items = [QtGui.QStandardItem(field) for field in row]
            ordered_items = [items[i] for i in ORDER]
            CSVmodel.appendRow(ordered_items)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eyllanesc