[PyQt] SystemLocaleShortDate omits the century on Linux

Sibylle Koczian nulla.epistola at web.de
Sat Mar 14 11:38:15 GMT 2020

Am 14.03.2020 um 10:57 schrieb J Barchan:
> /However/, I was screamed at over at the Qt forum.  The experts told me
> in no uncertain terms that this was /not/ the thing to do, to get a
> string format for a type which you wanted.  They insisted I should be
> doing it via QStyledItemDelegate, for whatever view I was in.  I should
> not handle the data(Qt.DisplayRole) and return a string, I should leave
> that (like Qt.EditRole) returning the underlying value type, not my
> desired format.  Indeed, they basically said the only time I should
> separate, say, DisplayRolefrom EditRole, is the kind of case quoted I
> think in the docs, where you have a spreadsheet with formulae and the
> thing you edit (the formula) is effectively quite a different data item
> from the number you end up displaying when evaluated.

I don't know what exactly the Qt experts had in mind, but for my
application overriding data (or setData) in fact creates a problem:

The data come from a PostgreSQL database and the date fields are
converted to QDate on import into the model (a QStandardItemModel
subclass). They are not edited in the table, but in a separate entry
form which contains a QDateEdit for the date field. This is only
sometimes changed, not in every edit of a record. After all the edits
are done, the model data are exported, the dates converted to strings
using the ISODate format, and then the database records are updated.

This is why I don't want to keep the dates as strings in the model: the
QDateEdit is quite comfortable to use, but it puts QDates into the model
- if and only if the date has been changed, as far as I can see. So I
get a mixture of strings and QDates in my exported list, which isn't
helpful. While QDates in the model can be used through all the
application, converted only at the start and at the end.

So only one question remains: how best to subclass QStyledItemDelegate.
Which method is the right one, where can I find an example which just
changes the displayed string format?

Thank you for hints,

More information about the PyQt mailing list