I’m trying to convert a QVariant to a QSize.
from PyQt4.QtCore import (
QVariant,
QSize,
)
s = QSize(4,3)
x = QVariant(s)
#Check, and hopefully do conversion
print x.canConvert(QVariant.Size) #True
print x.convert(QVariant.Size) #True
print type(x) #<class 'PyQt4.QtCore.QVariant'>...why??
print type(x.toSize()) #<class 'PyQt4.QtCore.QSize'>
Why do I have to do x.toSize for the type of x to be QSize? I expected x.convert() would make x an instance of QSize.
This wasn’t of much help to me… http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qvariant.html#convert
…because it says that convert() “casts the variant to the requested type…”
Thanks for the help!
You have misunderstood the purpose of QVariant.convert.
It converts the type of the variant value, not the
QVariantitself.To demonstrate:
The
to*()functions return the variant value converted to the requested type (if possible), but do not change the current type of theQVariantitself: