[PyQt] QVariant conversion toInt()

Christoph Burgmer chrislb at gmx.de
Sun Jul 27 17:05:36 BST 2008


Am Sonntag, 27. Juli 2008 schrieb lj:
> 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

myValue, _ = qv.toInt()    # get Phil's tuple

if you like. But in case you are unsure about the conversion you should really 
check the boolean var.

Chris


More information about the PyQt mailing list