<div dir="ltr"><div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, 6 Feb 2019 at 13:33, Maurizio Berti <<a href="mailto:maurizio.berti@gmail.com">maurizio.berti@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><div style="font-family:tahoma,sans-serif">If I did not make it clear I would remind you of one of my findings. I start with Python <span style="font-family:monospace,monospace">datetime.date</span> variables. I populate the model via <span style="font-family:monospace,monospace">setData(index, dateValue)</span> (no role specified, so <span style="font-family:monospace,monospace">EditRole</span>). At this point no sorting happens. However, all I have to do is change that to <span style="font-family:monospace,monospace">setData(index, QDate(dateValue))</span> and it <i>does</i> then work, so no "need to provide your own method".</div></div></div></div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><div style="font-family:tahoma,sans-serif"></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">Why is that? From <a href="http://doc.qt.io/qt-5/qsortfilterproxymodel.html#lessThan" target="_blank">http://doc.qt.io/qt-5/qsortfilterproxymodel.html#lessThan</a> I am told that the sorting deals in <span style="font-family:monospace,monospace">QVariant</span> types, and <span style="font-family:monospace,monospace">QMetaType::QDate</span> is among those it handles. Now, you know much better than I, but everywhere else I use PyQt it seems to convert between Python types and necessary <span style="font-family:monospace,monospace">QVariant</span> types invisibly and all is well, but not here? Why not?</div><div style="font-family:tahoma,sans-serif"><br></div></div></div></div></div></div></blockquote><div><br></div><div><div>Well, that's interesting: I've always assumed that the DisplayRole wouldn't accept data other than strings and numbers, but you made me realize that it can actually store any data that "supports" printable form (which QDate/Time do). Of course, this could be a small issue if you want a different string format for dates, but if the default one suits your needs, that's fine.<br>Since lessThan supports a small set of QVariant types (including QDate/Time) and we can use that QVariant type as a Display role, it's good enough.</div><div>Besides that, I tried to use setData with random QDateTimes converted QDate using their date() method, and it seems to work fine, without using QDate(dateValue) you mentioned.</div><div>To be clear, I tested with this code:</div><div><br></div><div><font face="monospace, monospace">sourceModel.setData(</font></div><div><font face="monospace, monospace"> sourceModel.index(row, 0), </font></div><div><font face="monospace, monospace"> QtCore.QDateTime().currentDateTime().addDays(-randrange(30)).addSecs(-randrange(86400)).date())</font></div><div> </div><div>I'm using an old Qt 5.7.1, it's possible that it's a regression, have you checked up on QtBugs?</div></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><div style="font-family:tahoma,sans-serif"></div><div style="font-family:tahoma,sans-serif">I'm also a little lost about <span style="font-family:monospace,monospace">QStandardItem</span> vs <span style="font-family:monospace,monospace">QStandardItemModel</span>, if that's significant. My code creates the (source) model as a <span style="font-family:monospace,monospace">QStandardItemModel</span>. That claims to use <a href="http://doc.qt.io/qt-5/qstandarditemmodel.html#setData" target="_blank">http://doc.qt.io/qt-5/qstandarditemmodel.html#setData</a>,whose default <span style="font-family:monospace,monospace">role</span> is <span style="font-family:monospace,monospace">EditRole</span>. Nowhere do I create a <span style="font-family:monospace,monospace">QStandardItem</span> explicitly, whose <a href="http://doc.qt.io/qt-5/qstandarditem.html#setData" target="_blank">http://doc.qt.io/qt-5/qstandarditem.html#setData</a> uses default <span style="font-family:monospace,monospace">role</span> is <span style="font-family:monospace,monospace">UserRole + 1</span>. So now I'm not sure which I am using, though I would think only <span style="font-family:monospace,monospace">QStandardItemModel</span>? Is this of relevance to my issue?<br></div></div></div></div></div></div></blockquote><div><br></div><div>Well, I've always used empty models at start and fill them as needed, since I mostly use dynamic models or prefer to create data in this way. But that's one way.<br>It doesn't change much from creating a model (even specifying the size, if you can) and then create new rows/columns and setting the data directly from the model.</div><div>In this case, the QStandardItems do not exist until the internal indexes are created (trying to access any index is enough, for example by setting or reading data): if you try to access a item(0, 0) on a new QStandardItemModel with a specified size, it will return None. If you try a itemFromIndex(model.index(0,0)) it will return an item.</div><div>That said, I don't think it would change much in your case, as soon as you always use the right role when setting data. I think that the default UserRole + 1 of item.setData is just for convenience, but since you always use the model.setData methods that wouldn't be an issue anyway.</div><div><br></div><div>Maurizio</div><div><br></div></div>-- <br><div dir="ltr" class="gmail-m_-2392710901102478890gmail_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></div></div></div>
</blockquote></div><br clear="all"><div><div style="font-family:tahoma,sans-serif" class="gmail_default">Hi Maurizio,</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Thank you again for your interest in replying.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="font-family:tahoma,sans-serif" class="gmail_default">Besides that, I tried to use setData with random QDateTimes converted
QDate using their date() method, and it seems to work fine, without
using QDate(dateValue) you mentioned.</div></blockquote><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">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 <span style="font-family:monospace,monospace">QDateTime</span>s to <span style="font-family:monospace,monospace">QDate</span>s. Please forget about <span style="font-family:monospace,monospace">QDateTime</span>, that is not the issue I am asking about. As long as your original code only uses the Qt native types like <span style="font-family:monospace,monospace">QDate</span> or <span style="font-family:monospace,monospace">QDateTime</span> there will be no problem sorting. I understand that.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">My code uses the <i>Python</i> types from the <span style="font-family:monospace,monospace">datetime</span> module. <span style="font-family:monospace,monospace">datetime.date</span> is a Python date type. In all other places I have made PyQt calls it always correctly converts between, say, Python type <span style="font-family:monospace,monospace">datetime.date</span> and Qt type <span style="font-family:monospace,monospace">QDate</span>, 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.<br></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">In the sole case of <span style="font-family:monospace,monospace">QSortFilterProxyModel.sort()</span>, however, if I fill my model via:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="font-family:tahoma,sans-serif" class="gmail_default"><span style="font-family:monospace,monospace">model.setData(row, col, something_of_type_datetime_date)</span><br></div></blockquote></div><div><br></div><div><div style="font-family:tahoma,sans-serif" class="gmail_default">everything about it works fine <i>except</i> for attempting to call the sort, which seems to do nothing to alter the order. If I change my model just so that I used:</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="gmail_default"><span style="font-family:monospace,monospace">model.setData(row, col, <span style="color:rgb(255,0,0)">QDate(</span>something_of_type_datetime_date)<span style="color:rgb(255,0,0)">)</span></span></div></blockquote><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">then the sorting also works.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">The <i>only</i> question I do not understand/would like answered is: why do I have to use <span style="font-family:monospace,monospace">QDate</span> and not <span style="font-family:monospace,monospace">datetime.date</span> in the model to make just <span style="font-family:monospace,monospace">QSortFilterProxyModel::sort()</span> work, when everything other than that works fine with <span style="font-family:monospace,monospace">datetime.date</span>? Why does PyQt auto-conversion between Python <span style="font-family:monospace,monospace">datetime.date</span> and Qt <span style="font-family:monospace,monospace">QDate</span> work everywhere other than during <span style="font-family:monospace,monospace">QSortFilterProxyModel.sort()</span>?<br></div><br></div><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><span style="font-family:tahoma,sans-serif">Kindest,</span></div><div><span style="font-family:tahoma,sans-serif">Jonathan</span></div></div></div></div></div></div>