Re: SystemLocaleShortDate omits the century on Linux

Maurizio Berti <[email protected]>
Newsgroups gmane.comp.python.pyqt-pykde
Message-ID <CAPn+-XRvF5CjUoj3YXjDM7FGvSJ9rJ9KEPhY9Ha-oHCfWY00fg@mail.gmail.com>
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
it.

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

    def setDateFormat(self, fmt=''):
        if not fmt:
            del self._dateFormat
        else:
            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):
            editor.setDisplayFormat(index.model().dateFormat())
        return editor


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

        formatEdit = QtWidgets.QLineEdit()
        layout.addWidget(formatEdit)

        table = QtWidgets.QTableView()
        layout.addWidget(table)

        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()
        proxy.setSourceModel(model)
        table.setModel(proxy)

        dateDelegate = DateDelegate()
        table.setItemDelegate(dateDelegate)

        formatEdit.setPlaceholderText(proxy.dateFormat())
        formatEdit.textChanged.connect(proxy.setDateFormat)


Cheers,
Maurizio

Il giorno ven 13 mar 2020 alle ore 15:48 Sibylle Koczian <
[email protected]> 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    [email protected]
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


-- 
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net

_______________________________________________
PyQt mailing list    [email protected]
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.