Question

I have a table with some regular text, some numbers and dates, the text sorts fine, but the numbers and the dates aren't, this is because the value given is a string, not an int or a QDate object, what do I have to implement to get the actual int and the actual QDate item to sort it properly?

Is it in the ItemModel or in the TableView? Do I have to subclass it?

Was it helpful?

Solution

Thanks to M4rtini I could implement the right function. For anyone strugling with it. Here's the code.

self.tableView = QtGui.TableView(self)
self.table_model = QtGui.QStandardItemModel(0, 0)
self.proxyModel = CustomSortingModel(self)

self.proxyModel.setSourceModel(self.table_model)
self.tableView.setModel(self.proxyModel)

class CustomSortingModel(QtGui.QSortFilterProxyModel):
    def lessThan(self,left,right):

        col = left.column()

        dataleft = left.data()
        dataright = right.data()

        if col == 2:
            dataleft = float(dataleft)
            dataright = float(dataright)
        elif col == 3:
            dataleft = QtCore.QDate.fromString(dataleft, "d/M/yy").addYears(100)
            dataright = QtCore.QDate.fromString(dataright, "d/M/yy").addYears(100)

        return dataleft < dataright
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top