<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">As far as I know, Unix "date +%x" returns the locale d_fmt, which is not necessarily the "shortest" form.<div>I believe that Qt relies on its own "super-set" of localizations, but I'm just guessing.</div><div><br></div><div>About your second question, you don't have to necessarily use a delegate, and subclassing is usually enough.</div><div>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.</div><div><br></div><div>Since you'll probably use a database as a source for the model, using QIdentityProxyModel is usually much better.</div><div>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.</div><div><br></div><div>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.</div><div><br></div><div><div><font face="monospace">class DateProxyModel(QtCore.QIdentityProxyModel):</font></div><div><font face="monospace">    def dateFormat(self):</font></div><div><font face="monospace">        try:</font></div><div><font face="monospace">            return self._dateFormat</font></div><div><font face="monospace">        except:</font></div><div><font face="monospace">            return QtCore.QLocale().dateFormat(QtCore.QLocale.ShortFormat)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def setDateFormat(self, fmt=''):</font></div><div><font face="monospace">        if not fmt:</font></div><div><font face="monospace">            del self._dateFormat</font></div><div><font face="monospace">        else:</font></div><div><font face="monospace">            self._dateFormat = fmt</font></div><div><font face="monospace">        # the date format has changed, notify all connected views about that</font></div><div><font face="monospace">        self.dataChanged.emit(QtCore.QModelIndex(), QtCore.QModelIndex())</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">    def data(self, index, role=QtCore.Qt.DisplayRole):</font></div><div><font face="monospace">        data = super().data(index, role)</font></div><div><font face="monospace">        if role == QtCore.Qt.DisplayRole and isinstance(data, QtCore.QDate):</font></div><div><font face="monospace">            return data.toString(self.dateFormat())</font></div><div><font face="monospace">        return data</font></div><div><font face="monospace"><br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace"><div>class DateProxyDelegate(QtWidgets.QStyledItemDelegate):</div><div>    def createEditor(self, parent, option, index):</div><div>        editor = super().createEditor(parent, option, index)</div><div>        if isinstance(editor, QtWidgets.QDateEdit) and isinstance(index.model(), DateProxyModel):</div><div>            editor.setDisplayFormat(index.model().dateFormat())</div><div>        return editor</div><div><br></div></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">class Test(QtWidgets.QWidget):</font></div><div><font face="monospace">    def __init__(self):</font></div><div><font face="monospace">        super().__init__()</font></div><div><font face="monospace">        layout = QtWidgets.QVBoxLayout(self)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">        formatEdit = QtWidgets.QLineEdit()</font></div><div><font face="monospace">        layout.addWidget(formatEdit)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">        table = QtWidgets.QTableView()</font></div><div><font face="monospace">        layout.addWidget(table)</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">        model = QtGui.QStandardItemModel()</font></div><div><font face="monospace">        model.setHorizontalHeaderLabels(['Date', 'Something'])</font></div><div><font face="monospace"><div><br></div></font></div><div><font face="monospace">        for row in range(10):</font></div><div><font face="monospace">            dateItem = QtGui.QStandardItem()</font></div><div><font face="monospace">            d = QtCore.QDate(randrange(2000, 2021), randrange(1, 13), randrange(1, 29))</font></div><div><font face="monospace">            dateItem.setData(d, QtCore.Qt.DisplayRole)</font></div><div><font face="monospace">            model.appendRow([dateItem, QtGui.QStandardItem('something')])</font></div><div><font face="monospace"><br></font></div><div><div><div><div><font face="monospace">        proxy = DateProxyModel()</font></div><div><font face="monospace">        proxy.setSourceModel(model)</font></div><div><font face="monospace">        table.setModel(proxy)</font></div><div><font face="monospace"><br></font></div><div><div></div></div><font face="monospace"><div>        dateDelegate = DateDelegate()</div><div>        table.setItemDelegate(dateDelegate)</div><div></div></font></div><div><font face="monospace"></font></div></div><div></div><div><font face="monospace"><br></font></div><div><div><div><font face="monospace">        formatEdit.setPlaceholderText(proxy.dateFormat())</font></div><div><font face="monospace">        formatEdit.textChanged.connect(proxy.setDateFormat)</font></div></div><div></div><font face="monospace"><div><br></div><div><br></div></font></div></div></div><div>Cheers,</div><div>Maurizio</div></div></div></div></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno ven 13 mar 2020 alle ore 15:48 Sibylle Koczian <<a href="mailto:nulla.epistola@web.de">nulla.epistola@web.de</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Am 11.03.2020 um 20:47 schrieb Maurizio Berti:<br>
> It completely depends on how the system "tells" what is the date format.<br>
> Apparently, Windows defaults to 4 digits for your localization, while <br>
> it's set to 2 for Linux.<br>
<br>
So I thought too, but then why does "date +%x", which should show "the <br>
locale's date representation", give all 4 digits? This is very confusing.<br>
<br>
> You can change them in both systems: in Windows it's on the regional <br>
> settings, while on Linux it depends on your distro/window manager/etc.<br>
> <br>
<br>
In this case Windows does it right. I'll have yet to find the right <br>
setting for Linux. But in the meantime: if I want a QTableView to render <br>
QDate values in another format, a custom delegate is the way to go, <br>
right? But which method? All the examples I've seen for overwritten <br>
paint() methods seem unnecessary complicated for this sort of small change.<br>
<br>
Sibylle<br>
<br>
_______________________________________________<br>
PyQt mailing list    <a href="mailto:PyQt@riverbankcomputing.com" target="_blank">PyQt@riverbankcomputing.com</a><br>
<a href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div>