[PyQt] Incorrect conversion from datetime to QVariant

Phil Thompson phil at riverbankcomputing.com
Sat Oct 11 21:27:40 BST 2008


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()))

Phil


More information about the PyQt mailing list