[PyQt] QVariant conversion toInt()
lj
larry at foxgulch.com
Sun Jul 27 16:23:48 BST 2008
Thanks for your responses. I've read and re-read the doc. But at the
risk of being labeled as a "Language Lawyer, junior grade",
I guess the correct way to retrieve ones "int" from a QVariant is
something like this?
from PyQt4.QtCore import *
from PyQt4.QtGui import *
qv = QVariant(255) # qv contains integer 255
if qv.canConvert(QVariant.Int): # if castable then
returnedTuple = qv.toInt() # get Phil's tuple
myValue = returnedTuple[0] # pick off the integer
else:
myValue = None #flag error or set default value
print myValue, type(myValue) #prints 255 and <type 'int'>
And furthermore I read where string can be converted to just
about everything... except Point, Rect and Size so to check
I ran the script below.
qv = QVariant(QString("255")) # qv contains QString "255"
print "\n Expect False"
print qv.canConvert(QVariant.Point)
print qv.canConvert(QVariant.Rect)
print qv.canConvert(QVariant.Size)
print "\n Expect True"
print qv.canConvert(QVariant.Bool)
print qv.canConvert(QVariant.ByteArray)
print qv.canConvert(QVariant.Char) #!!! Prints False !!!
print qv.canConvert(QVariant.Color)
print qv.canConvert(QVariant.Date)
print qv.canConvert(QVariant.DateTime)
print qv.canConvert(QVariant.Double)
print qv.canConvert(QVariant.Font)
print qv.canConvert(QVariant.Int)
print qv.canConvert(QVariant.LongLong)
print qv.canConvert(QVariant.StringList)
print qv.canConvert(QVariant.Time)
print qv.canConvert(QVariant.UInt)
print qv.canConvert(QVariant.ULongLong)
All print True or False as expected except for the QVariant.Char
attempt which prints "False" where "True" was expected
More information about the PyQt
mailing list