<div dir="ltr"><div dir="ltr"><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><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">Any kind of object can be assigned to a (any) DataRole to a valid QModelIndex.</div></blockquote><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">I suspect this is the key to my misunderstanding. When I pass a Python <span style="font-family:monospace,monospace">datetime</span> to some PyQt function which expects a <span style="font-family:monospace,monospace">QDate</span> (I can't think of one), or a Python <span style="font-family:monospace,monospace">decimal.Decimal</span> where PyQt/Qt function expects a <span style="font-family:monospace,monospace">float</span>, they get auto-converted, right? </div></div></div></blockquote><div><br></div><div>I think that "basic" standard data types are used transparently between Python and Qt. I don't know how this exactly works behind the scenes (expecially for "advanced" numeric types, like qint8 or qlonglong), I think that standard types like int, float and strings are just used as they are represented internally by the Python interpreter, while PyQt automatically offers automatic conversion for more advanced classes whenever it makes sense.<br><br></div><div>In the old PyQt4 docs, it explicitly says:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">A Python date object may be used whenever a QDate is expected.</blockquote><div>The same appears for QDateTime and QTime, but for other "compatible" types also, like when using string objects for QByteArrays.<br></div><div><br></div><div>This means that you can use a Python date object as an argument for any method that expects a QDate (for example, the QDateTimeEdit.setDate()), including the QDate __init__ itself (that's your case), since new QDate objects can be also created using the QDate(QDate) initialization method.</div><div>So, a Python date/time is "accepted" as an argument and automatically converted to a Qt date/time object for the meanings of the method called. The original object is unchanged and there's no reference to it from the Qt side of things.</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><div style="font-family:tahoma,sans-serif">But when I pass those to, say, <span style="font-family:monospace,monospace">QStandardItemModel.setData()</span>, you are saying there is no conversion and the Python object is stored as-is, right? And when <span style="font-family:monospace,monospace">QStandardItemModel.sort()/lessThan()</span> comes along and looks at the data which is a Python <span style="font-family:monospace,monospace">datetime</span> or <span style="font-family:monospace,monospace">Decimal</span> type, this does not count as the same situation (e.g. explicit argument to function) where PyQt would auto-convert as necessary --- quite possibly because we are down in C++ Qt code by then which knows nothing about Python --- so it fails. Is that about right?</div></div></div></blockquote><div><br></div><div>Quite correct. Remember that an item model doesn't care about the type of data stored (and it shouldn't), it's only an abstract categorization of a collection of objects.</div><div>Think of it as an advanced Python list, where you could put any kind of object in it. It wouldn't make sense to "change" the object type when you add it to the list, right?</div><div>The only scenarios in which the data type becomes important is when the data is represented (in an item view) or some level of interaction is required, such as finding data or comparing. For visual representation it behaves like printing the Python list: if the object has __str__ implemented it will show the returned string, even if the object is not a string, so it will display the content if the object is "known" as printable for Qt; but if you try to sort a non builtin Python type, you will likely get back the original sorting, since it doesn't know how to handle the data.</div><div><br></div><div>I wanted to do a couple of test to better demonstrate all this, but it seems that I'm not able to automatically display a Python date object in a QTableView (I thought I did it while testing yesterday, but maybe I was wrong), requiring an item delegate to keep the original object and actually display the date.<br>Just out of curiosity, did you actually see the dates in the fields for which you used Python date objects as setData argument (without the QDate conversion)?</div><div><br></div><div>Finally, about your last sentence, it's not that "Qt knows nothing about Python". The model can "see" the stored object, but it's an unknown data type (remember, it has not been "converted", when storing data to a model it should not change its contents), thus the it has no way to know how to sort it.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
</blockquote></div><br>When sorting, models usually call the private method isVariantLessThan() you can see here:</div><div dir="ltr"><a href="https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEb">https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEb</a></div><div>You'll see that the default case switch behaves as the QVariant string type, trying to transform the QVariant to a string. Since PyQt5 hides some features due to the "transparency" of data types (there are no QStrings nor QVariants by default), I'll show this with PyQt4, which probably better explains what happens under the hood. Remember that with SIP v1 (the default for PyQt4) data models usually returned QVariants, requiring the transformation to toPyObject to get the original form.</div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">>>> from PyQt4 import QtCore
</span><br>>>> intVariant = QtCore.QVariant(float(5))
<br>>>> intVariant.typeName()
<br>'double'
<br>>>> intVariant.toString()
<br>PyQt4.QtCore.QString(u'5')
<br>>>> from datetime import date
<br>>>> dateVariant = QtCore.QVariant(date.today())
<br>>>> dateVariant.typeName()
<br>'PyQt_PyObject'
<br>>>> dateVariant.toString()
<br>PyQt4.QtCore.QString(u'')<br></span></div><div dir="ltr"><br></div>As you can see, the intVariant is correctly "transformed" to an internal double type and, while isVariantLessThan will obviously use the default numeric comparison, its toString method returns the string representation. In the second case, the QVariant type is indeed a "special" Qt object type, and its string is null. Some might object that, if the object has a __str__ method implemented, it should return that, but that's open to (another) debate.<div>At this point, lessThan will always receive False for unknown data types, leaving the whole sorting unchanged.</div><div><br></div><div>Well, that was educational for me too :-)<br>Hope this helps you as well!</div><div><br></div><div>Regards,</div><div>Maurizio</div><div dir="ltr"><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></div></div></div></div></div>