<div dir="ltr"><div dir="ltr"><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 class="gmail_attr" dir="ltr">On Thu, 7 Feb 2019 at 23:22, 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;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><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;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><div dir="ltr"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><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;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">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;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid"><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;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">
</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" target="_blank">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 class="gmail-m_-4954546431677111923gmail_signature" dir="ltr">È 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>
</blockquote></div><br clear="all"><div class="gmail_default" style="font-family:tahoma,sans-serif">Hi Maurizio,</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Yet again, thank you for your interest.  Though my head is beginning to ache over all this now! :)</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Again, I <em>think</em> you may have hit the nail on the head when you write:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">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)?</blockquote></div><div><br></div><div>Until yesterday, I would have claimed that, yes, the <font face="monospace,monospace">QTableView</font> does show these python <font face="monospace,monospace">datetime</font>s from the model natively.  Then I noticed in something new I was doing that they were coming out blank.  I am beginning to believe that you are quite right and it does <em>not</em> auto-handle/convert them....</div><div><br></div><div>Please understand: until a year or so ago I had never used Python or Qt.  Now I develop & maintain (on my own, so no one I can ask) an existing PyQt project with tens of thousands of lines of existing code.  Written by different people over years with not a single comment and impenetrable logic.  This means I don't always know what's happening where.</div><div><br></div><div>Now, if my understanding that PyQt was auto-converting <font face="monospace,monospace">datetime</font> <-> <font face="monospace,monospace">QDate</font>, at least in the context of model data/table view, turns out to be baseless, then this all begins to make a lot  more sense.  If the table view won't display a Python <font face="monospace,monospace">datetime</font>, because it knows nothing about it, then I can appreciate it will have similar problem not being able to sort by it.</div></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Since you are enjoying yourself(!), I also claimed that a column with Python type <font face="monospace,monospace">decimal.Decimal</font> in it (that's what we use for all our monetary numbers, we need two decimal places and no loss of precision so <font face="monospace,monospace">float</font> is no good) fails to sort in just the same way as <font face="monospace,monospace">dateime.date</font>.  Again, there I <em>thought</em> some auto-conversion was going to <font face="monospace,monospace">float</font> which the sort does know about.  You <em>might</em> like to see how that behaves for you?  If that too does not sort and does not display correctly (i.e. it's not handled intrinsically by model/view code), then again I wonder whether we don't have our own explicit code in, say, <font face="monospace,monospace">.data(row, col, Qt.DisplayRole)</font> which converts to a fixed-2-decimals string output.  So again if it can't be displayed natively I would understand why it can't be sorted on.</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">I would do this myself today if I could.  However as of yesterday our whole web site no longer works, following a move by our ISP from PHP 5.6 to 7.x.  Reading up, I discover that for years until PHP 5.6 it used a "mysql" extension library for all database interaction, and now that has been removed and replaced with a new "mysqli" extension library, which is different, so our whole web site cannot access its database (inc. WordPress).  So today, having never done any PHP and knowing nothing about how it all works, I must urgently learn and fix the whole site... trust you understand!</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Ciao, e grazie :)</div><br>-- <br><div class="gmail_signature" dir="ltr"><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></div></div>