[PyQt] Do not avto convert python date to QVariant

Phil Thompson phil at riverbankcomputing.com
Tue May 26 12:56:30 BST 2009


On Tue, 26 May 2009 16:49:10 +0700, Alexandr N Zamaraev <tonal at promsoft.ru>
wrote:
> [code]
> from PyQt4 import QtCore
> import datetime
> 
> def show(d, QtType):
>    print 'pytype:', d
>    v = QtCore.QVariant(d)
>    print 'from variant:', str(QtCore.QVariant(d).toString())
>    print 'from qttype:', str(QtCore.QVariant(QtType(d)).toString())
> 
> show(datetime.date.today(), QtCore.QDate)
> print
> show(datetime.datetime.today(), QtCore.QDateTime)
> print
> show(datetime.datetime.today().time(), QtCore.QTime)
> [/code]
> In PyQt 4.4.4 all converts.

This is a deliberate change. It is documented - but I'll expand the
documentation to mention the datetime types explicitly. Use toPyObject() to
get the Python datetime instances back.

If you want the old behaviour then convert the Python types to their Qt
equivalents before converting to QVariant - and that will work Ok with
earlier versions as well.

Phil


More information about the PyQt mailing list