[PyQt] Odd sorting behaviour in QSortProxyModel

David Townshend aquavitae69 at gmail.com
Wed Jan 11 06:55:14 GMT 2012


Hi

I've found something rather odd that happens when sorting in a
QSortFilterProxyModel with datetime.date datatypes.  If I call sort()
on a column containing datetime.date values, it doesn't appear to do
anything.  If I then call sort on another column, then on the first
column again, it works.  I am using python 3.2 and PyQt 4.8.3 on
Windows XP. I haven't tried this on any other platform.  Does anyone
else get this behaviour?

The following code shows illustrates the problem:

from datetime import date
from PyQt4.QtCore import QAbstractTableModel, Qt, QDate
from PyQt4.QtGui import QSortFilterProxyModel

class Model(QAbstractTableModel):

    def rowCount(self, parent=None):
        return 4

    def columnCount(self, parent=None):
        return 2

    def data(self, index, role):
        if role != Qt.DisplayRole:
            return super().data(index, role)
        values = [(11, date(2011, 1, 1)),
                  (10, date(2010, 1, 1)),
                  (14, date(2014, 1, 1)),
                  (5, date(2005, 1, 1))]
        return values[index.row()][index.column()]

model = Model()
sortModel = QSortFilterProxyModel()
sortModel.setSourceModel(model)

col = 1

print("These should be sorted, but aren't")
sortModel.sort(col)
for row in range(4):
    print(sortModel.data(sortModel.index(row, col), Qt.DisplayRole))

print("Now they are sorted")
sortModel.sort(0)
sortModel.sort(col)
for row in range(4):
    print(sortModel.data(sortModel.index(row, col), Qt.DisplayRole))


More information about the PyQt mailing list