<div dir="ltr"><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 14:47, 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><div style="font-family:tahoma,sans-serif">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.<br></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">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></div></blockquote><div><br></div><div>I'm sorry, Jonathan: I think I didn't read your first message carefully enough, and then I didn't take the "Python type datetime" into account.</div><div><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><div style="font-family:tahoma,sans-serif">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">Q<span class="gmail_default" style="font-family:tahoma,sans-serif"></span>SortFilterProxyModel::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></div></div></blockquote></div><div><br>Well, I'm just guessing an answer here; it's possible that in every other situation the conversion is done automatically since you're doing operations involving unambiguous treatment of the date object, for example when setting a QDateTimeEdit date, or, in this case, display the date in the table field.</div><div>The sort() method instead attempts to sort by "guessing" the preferred sorting criteria according to the content of the field, which is not unique. You could have strings, floats and datetimes in the same column and *maybe* that's why the automatic conversion doesn't work properly in this case: while QDateTime is directly recognized by lessThan() along with other QVariant types, Python's datetime is not "native" to Qt and I suppose that it just doesn't know what to do with it (do you want to compare it as a string? as a number? as a QDate? as a QDateTime?), then I think it just "leaves" the object as it is, without doing any comparison.</div><div><br>Again, this is just a guess, tonight I'll check the sources and maybe I'll find out a better answer.</div><div dir="ltr"><br></div>Maurizio<br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_-6147876034842764106gmail_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><div><br></div><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><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">tonight I'll check the sources and maybe I'll find out a better answer.</div></blockquote><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">That would be most interesting to me, if you feel like doing it.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Think of this.  When I go <span style="font-family:monospace,monospace">model.setData(row, col, python_date)</span>, the C++ definition for <span style="font-family:monospace,monospace">setData()</span> takes a <span style="font-family:monospace,monospace">QVariant</span> for the value.  Somewhere along the line, PyQt auto-converts Python <span style="font-family:monospace,monospace">datetime.date</span> to a <span style="font-family:monospace,monospace">QVariant</span> of type <span style="font-family:monospace,monospace">QMetaType::QDate</span> containing a <span style="font-family:monospace,monospace">QDate</span>, right?  (And similarly unwraps if I call <span style="font-family:monospace,monospace">model.data(row, col)</span>.)  At least that's my understanding.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">When I go <span style="font-family:monospace,monospace"><span class="gmail_default" style="font-family:tahoma,sans-serif"></span>SortFilterProxyModel::sort()</span>, I read the documentation at <a href="http://doc.qt.io/qt-5/qsortfilterproxymodel.html#lessThan">http://doc.qt.io/qt-5/qsortfilterproxymodel.html#lessThan</a>.  I see <span style="font-family:monospace,monospace">QMetaType::QDate</span> among the <span style="font-family:monospace,monospace">QVariant</span> types it says it can handle.  When it comes to compare the values in my model, I'm <i>expecting</i> it to similarly come across my Python <span style="font-family:monospace,monospace">datetime.date</span> values and auto-convert them to that <span style="font-family:monospace,monospace">QVariant</span> which it knows it can handle.  But it does not appear to do that.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Meanwhile, that doc states it can also handle <span style="font-family:monospace,monospace">QMetaType::QString</span>.  <span style="font-family:monospace,monospace">QString</span> is also a Qt type, not a Python one.  But I don't bother to put <span style="font-family:monospace,monospace">QString</span>s into my model, I just put in plain Python <span style="font-family:monospace,monospace">str</span>s, <i>and yet I find that those <u>do</u> sort</i>.  Why is that OK but the date ones not?<br></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">(Hmm, I'm beginning to wonder/think: the example I gave with <span style="font-family:monospace,monospace">setData()/data()</span> it must do whatever converting on the argument/return result there & then, from/to Python.  With the <span style="font-family:monospace,monospace">SortFilterProxyModel::sort()</span>, encountering values is going on <i>during</i> the sort, which is written in C++, so that code will not know about Python type conversions.  Is this what it's all about, the reason why no conversion takes place during <span style="font-family:monospace,monospace">sort()</span> only?)<br></div></div><div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">To make it even more confusing.  The above doc link for <span style="font-family:monospace,monospace">SortFilterProxyModel::lessThan()</span> states:</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">Any other type will be converted to a <a href="http://doc.qt.io/qt-5/qstring.html">QString</a> using <a href="http://doc.qt.io/qt-5/qvariant.html#toString">QVariant::toString</a>().</div></blockquote><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">I therefore thought if my native Python <span style="font-family:monospace,monospace">datetime.date</span> was "not recognised", it would at minimum do a string sort.  Might not be what I intended for my dates, but at least I'd see something happening in the way of reordering.  Instead, I see nothing gets moved, the original order is simply preserved, just as though for whatever reason the sort has done nothing.  No complaints, just nothing happens.</div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><div style="font-family:tahoma,sans-serif" class="gmail_default">Thanks for your time!<br></div><div style="font-family:tahoma,sans-serif" class="gmail_default"><br></div><br></div><br>-- <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></div>