[PyQt] Incorrect conversion from datetime to QVariant

Phil Thompson phil at riverbankcomputing.com
Sun Oct 12 13:09:39 BST 2008


On Sat, 11 Oct 2008 21:27:40 +0100, Phil Thompson
<phil at riverbankcomputing.com> wrote:
> On Fri, 10 Oct 2008 16:40:39 +0700, Alexandr N Zamaraev
<tonal at promsoft.ru>
> wrote:
>> My Environment:
>> WinVista Home Basic Ru + sp1
>> Python 2.5.2
>> sip 4.7.7 (from source, build gcc mingw 3.4.5)
>> Qt 4.4.1 (from source, build gcc mingw 3.4.5)
>> PyQt 4.4.3 (from source, build gcc mingw 3.4.5)
>> 
>> Code snap:
>>  >>> from datetime import datetime
>>  >>> from PyQt4 import QtCore
>>  >>> u'%s' % datetime.today()
>> u'2008-10-10 16:37:57.822000'
>>  >>> '%s' % QtCore.QVariant(datetime.today()).toString()
>> u'2008-10-10'
>>  >>> QtCore.QVariant(datetime.today()).typeName()
>> 'QDate'
>> 
>> Why QtCore.QVariant(datetime.today()).typeName() is 'QDate'?
>> Mast by 'QDateTime'.
> 
> This is fixed in the current snapshot, but the behaviour is slightly
> different.
> 
> You will actually get back (via toPyObject()) a reference to the original
> datetime object. If you really want the QVariant to contain a QDateTime
> then do this...
> 
>     v = QtCore.QVariant(QtCore.QDateTime(datetime.today()))

I've just changed things so that you now get back a QDateTime instead of
the original datetime object.

I'm trying to fix aspects of QVariant but without affecting backwards
compatibility, but it's a delicate balance.

Phil


More information about the PyQt mailing list