[PyQt] SystemLocaleShortDate omits the century on Linux

Maurizio Berti maurizio.berti at gmail.com
Fri Mar 13 21:23:49 GMT 2020

As far as I know, Unix "date +%x" returns the locale d_fmt, which is not
necessarily the "shortest" form.
I believe that Qt relies on its own "super-set" of localizations, but I'm
just guessing.

About your second question, you don't have to necessarily use a delegate,
and subclassing is usually enough.
If you're using a QStandardItemModel, you only have to subclass it and
override data(): if the source data is actually a QDate, return the format
that better suits your needs.

Since you'll probably use a database as a source for the model, using
QIdentityProxyModel is usually much better.
In this example I'll fill a simple table with a date column, and a subclass
of QIdentityProxyModel returns the data in the selected format whenever a
DisplayRole returns a QDate.

In this way you don't need a delegate, if not for the editor; I also added
a "companion" delegate for the proxy, so that if it is going to show a
QDateEdit and the model is the proxy, it will use the same format set for

class DateProxyModel(QtCore.QIdentityProxyModel):
    def dateFormat(self):
            return self._dateFormat
            return QtCore.QLocale().dateFormat(QtCore.QLocale.ShortFormat)

    def setDateFormat(self, fmt=''):
        if not fmt:
            del self._dateFormat
            self._dateFormat = fmt
        # the date format has changed, notify all connected views about that
        self.dataChanged.emit(QtCore.QModelIndex(), QtCore.QModelIndex())

    def data(self, index, role=QtCore.Qt.DisplayRole):
        data = super().data(index, role)
        if role == QtCore.Qt.DisplayRole and isinstance(data, QtCore.QDate):
            return data.toString(self.dateFormat())
        return data

class DateProxyDelegate(QtWidgets.QStyledItemDelegate):
    def createEditor(self, parent, option, index):
        editor = super().createEditor(parent, option, index)
        if isinstance(editor, QtWidgets.QDateEdit) and
isinstance(index.model(), DateProxyModel):
        return editor

class Test(QtWidgets.QWidget):
    def __init__(self):
        layout = QtWidgets.QVBoxLayout(self)

        formatEdit = QtWidgets.QLineEdit()

        table = QtWidgets.QTableView()

        model = QtGui.QStandardItemModel()
        model.setHorizontalHeaderLabels(['Date', 'Something'])

        for row in range(10):
            dateItem = QtGui.QStandardItem()
            d = QtCore.QDate(randrange(2000, 2021), randrange(1, 13),
randrange(1, 29))
            dateItem.setData(d, QtCore.Qt.DisplayRole)
            model.appendRow([dateItem, QtGui.QStandardItem('something')])

        proxy = DateProxyModel()

        dateDelegate = DateDelegate()



Il giorno ven 13 mar 2020 alle ore 15:48 Sibylle Koczian <
nulla.epistola at web.de> ha scritto:

> Am 11.03.2020 um 20:47 schrieb Maurizio Berti:
> > It completely depends on how the system "tells" what is the date format.
> > Apparently, Windows defaults to 4 digits for your localization, while
> > it's set to 2 for Linux.
> So I thought too, but then why does "date +%x", which should show "the
> locale's date representation", give all 4 digits? This is very confusing.
> > You can change them in both systems: in Windows it's on the regional
> > settings, while on Linux it depends on your distro/window manager/etc.
> >
> In this case Windows does it right. I'll have yet to find the right
> setting for Linux. But in the meantime: if I want a QTableView to render
> QDate values in another format, a custom delegate is the way to go,
> right? But which method? All the examples I've seen for overwritten
> paint() methods seem unnecessary complicated for this sort of small change.
> Sibylle
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt

È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20200313/bfa6ca1f/attachment.htm>

More information about the PyQt mailing list