[PyQt] QSortFilterProxyModel::sort() not sorting on column types
Maurizio Berti
maurizio.berti at gmail.com
Wed Feb 6 14:46:31 GMT 2019
>
> This does not matter now, in that I have a workaround, but I'll ask you
> one more time. The example you give talks about converting QDateTimes to
> QDates. Please forget about QDateTime, that is not the issue I am asking
> about. As long as your original code only uses the Qt native types like
> QDate or QDateTime there will be no problem sorting. I understand that.
>
> My code uses the *Python* types from the datetime module. datetime.date
> is a Python date type. In all other places I have made PyQt calls it
> always correctly converts between, say, Python type datetime.date and Qt
> type QDate, in either direction, as necessary. I do not have to do
> anything special in my code. The PyQt wrappers for arguments etc. are
> doing it for me. This is a huge (good) point about PyQt.
>
I'm sorry, Jonathan: I think I didn't read your first message carefully
enough, and then I didn't take the "Python type datetime" into account.
The *only* question I do not understand/would like answered is: why do I
> have to use QDate and not datetime.date in the model to make just
> QSortFilterProxyModel::sort() work, when everything other than that works
> fine with datetime.date? Why does PyQt auto-conversion between Python
> datetime.date and Qt QDate work everywhere other than during
> QSortFilterProxyModel.sort()?
>
Well, I'm just guessing an answer here; it's possible that in every other
situation the conversion is done automatically since you're doing
operations involving unambiguous treatment of the date object, for example
when setting a QDateTimeEdit date, or, in this case, display the date in
the table field.
The sort() method instead attempts to sort by "guessing" the preferred
sorting criteria according to the content of the field, which is not
unique. You could have strings, floats and datetimes in the same column and
*maybe* that's why the automatic conversion doesn't work properly in this
case: while QDateTime is directly recognized by lessThan() along with other
QVariant types, Python's datetime is not "native" to Qt and I suppose that
it just doesn't know what to do with it (do you want to compare it as a
string? as a number? as a QDate? as a QDateTime?), then I think it just
"leaves" the object as it is, without doing any comparison.
Again, this is just a guess, tonight I'll check the sources and maybe I'll
find out a better answer.
Maurizio
--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190206/232a594d/attachment.html>
More information about the PyQt
mailing list