On Thu, 7 Feb 2019 at 23:22, Maurizio Berti <maurizio.berti at gmail.com>
wrote:
> Any kind of object can be assigned to a (any) DataRole to a valid
>>> QModelIndex.
>>>>>>> I suspect this is the key to my misunderstanding. When I pass a Python
>> datetime to some PyQt function which expects a QDate (I can't think of
>> one), or a Python decimal.Decimal where PyQt/Qt function expects a float,
>> they get auto-converted, right?
>>>> 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.
>> In the old PyQt4 docs, it explicitly says:
>>> A Python date object may be used whenever a QDate is expected.
>> The same appears for QDateTime and QTime, but for other "compatible" types
> also, like when using string objects for QByteArrays.
>> 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.
> 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.
>>>> But when I pass those to, say, QStandardItemModel.setData(), you are
>> saying there is no conversion and the Python object is stored as-is,
>> right? And when QStandardItemModel.sort()/lessThan() comes along and
>> looks at the data which is a Python datetime or Decimal 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?
>>>> 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.
> 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?
> 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.
>> 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.
> 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)?
>> 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.
>>>> When sorting, models usually call the private method isVariantLessThan()
> you can see here:
>> https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qabstractitemmodel.cpp.html#_ZN25QAbstractItemModelPrivate17isVariantLessThanERK8QVariantS2_N2Qt15CaseSensitivityEb
> 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.
>> >>> from PyQt4 import QtCore
> >>> intVariant = QtCore.QVariant(float(5))
> >>> intVariant.typeName()
> 'double'
> >>> intVariant.toString()
> PyQt4.QtCore.QString(u'5')
> >>> from datetime import date
> >>> dateVariant = QtCore.QVariant(date.today())
> >>> dateVariant.typeName()
> 'PyQt_PyObject'
> >>> dateVariant.toString()
> PyQt4.QtCore.QString(u'')
>> 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.
> At this point, lessThan will always receive False for unknown data types,
> leaving the whole sorting unchanged.
>> Well, that was educational for me too :-)
> Hope this helps you as well!
>> Regards,
> Maurizio
>> --
> 脠 difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
>
Hi Maurizio,
Yet again, thank you for your interest. Though my head is beginning to
ache over all this now! :)
Again, I *think* you may have hit the nail on the head when you write:
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.
> 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)?
Until yesterday, I would have claimed that, yes, the QTableView does show
these python datetimes 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 *not* auto-handle/convert
them....
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.
Now, if my understanding that PyQt was auto-converting datetime <-> QDate,
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 datetime, because it knows nothing about it, then I can
appreciate it will have similar problem not being able to sort by it.
Since you are enjoying yourself(!), I also claimed that a column with
Python type decimal.Decimal in it (that's what we use for all our monetary
numbers, we need two decimal places and no loss of precision so float is no
good) fails to sort in just the same way as dateime.date. Again, there I
*thought* some auto-conversion was going to float which the sort does know
about. You *might* 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, .data(row, col, Qt.DisplayRole) 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.
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!
Ciao, e grazie :)
--
Kindest,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/pyqt/attachments/20190208/c9c62a44/attachment-0001.html>
More information about the PyQt
mailing list
‘She has never mentioned her father to me. Was he—well, the sort of man whom the County Club would not have blackballed?’ "We walked by the side of our teams or behind the wagons, we slept on the ground at night, we did our own cooking, we washed our knives by sticking them into the ground rapidly a few times, and we washed our plates with sand and wisps of grass. When we stopped, we arranged our wagons in a circle, and thus formed a 'corral,' or yard, where we drove our oxen to yoke them up. And the corral was often very useful as a fort, or camp, for defending ourselves against the Indians. Do you see that little hollow down there?" he asked, pointing to a depression in the ground a short distance to the right of the train. "Well, in that hollow our wagon-train was kept three days and nights by the Indians. Three days and nights they stayed around, and made several attacks. Two of our men were killed and three were wounded by their arrows, and others had narrow escapes. One arrow hit me on the throat, but I was saved by the knot of my neckerchief, and the point only tore the skin a little. Since that time I have always had a fondness for large neckties. I don't know how many of the Indians we killed, as they carried off their dead and wounded, to save them from being scalped. Next to getting the scalps of their enemies, the most important thing with the Indians is to save their own. We had several fights during our journey, but that one was the worst. Once a little party of us were surrounded in a small 'wallow,' and had a tough time to defend ourselves successfully. Luckily for us, the Indians had no fire-arms then, and their bows and arrows were no match for our rifles. Nowadays they are well armed, but there are[Pg 41] not so many of them, and they are not inclined to trouble the railway trains. They used to do a great deal of mischief in the old times, and many a poor fellow has been killed by them." As dusk came on nearly the whole population of Maastricht, with all their temporary guests, formed an endless procession and went to invoke God's mercy by the Virgin Mary's intercession. They went to Our Lady's Church, in which stands the miraculous statue of Sancta Maria Stella Maris. The procession filled all the principal streets and squares of the town. I took my stand at the corner of the Vrijthof, where all marched past me, men, women, and children, all praying aloud, with loud voices beseeching: "Our Lady, Star of the Sea, pray for us ... pray for us ... pray for us ...!" It had not occurred to her for some hours after Mrs. Campbell had told her of Landor's death that she was free now to give herself to Cairness. She had gasped, indeed, when she did remember it, and had put the thought away, angrily and self-reproachfully. But it returned now, and she felt that she might cling to it. She had been grateful, and she had been faithful, too.[Pg 286] She remembered only that Landor had been kind to her, and forgot that for the last two years she had borne with much harsh coldness, and with a sort of contempt which she felt in her unanalyzing mind to have been entirely unmerited. Gradually she raised herself until she sat quite erect by the side of the mound, the old exultation of her half-wild girlhood shining in her face as she planned the future, which only a few minutes before had seemed so hopeless. After he had gloated over Sergeant Ramsey, Shorty got his men into the road ready to start. Si placed himself in front of the squad and deliberately loaded his musket in their sight. Shorty took his place in the rear, and gave out: The groups about each gun thinned out, as the shrieking fragments of shell mowed down man after man, but the rapidity of the fire did not slacken in the least. One of the Lieutenants turned and motioned with his saber to the riders seated on their horses in the line of limbers under the cover of the slope. One rider sprang from each team and ran up to take the place of men who had fallen. "As long as there's men and women in the world, the men 'ull be top and the women bottom." Then, in the house, the little girls were useful. Mrs. Backfield was not so energetic as she used to be. She had never been a robust woman, and though her husband's care had kept her well and strong, her frame was not equal to Reuben's demands; after fourteen years' hard labour, she suffered from rheumatism, which though seldom acute, was inclined to make her stiff and slow. It was here that Caro and Tilly came in, and Reuben began to appreciate his girls. After all, girls were needed in a house—and as for young men and marriage, their father could easily see that such follies did not spoil their usefulness or take them from him. Caro and Tilly helped their grandmother in all sorts of ways—they dusted, they watched pots, they shelled peas and peeled potatoes, they darned house-linen, they could even make a bed between them. HoME一级毛片视频免费公开
ENTER NUMBET 0018www.jadnet.com.cn www.vyqh.com.cn www.svux.com.cn www.idctop.net.cn www.cgolden.com.cn gpij.com.cn kamr.com.cn www.wa-ka.com.cn leadnews.com.cn sh-ysjs.com.cn