[PyQt] QVariant conversion toInt()

Phil Thompson phil at riverbankcomputing.com
Sun Jul 27 09:17:50 BST 2008


On Sat, 26 Jul 2008 19:51:55 -0600, lj <larry at foxgulch.com> wrote:
> Given this small snip:
> 
> from PyQt4.QtCore  import *
> from PyQt4.QtGui import *
> 
> qv = QVariant(255)
> print qv.canConvert(QVariant.Int)  #prints True, can be converted
> x =  qv.toInt()                    # so attempt the conversion
> print type(x), x                  #prints <type 'tuple'>  (255, True)
>                                   # I expected  <type int>  and  255
> 
> I supposed that x after conversion out of the variant object qv would be
> type Int  and  print x  would  result in 255 being printed but that
> doesn't happen.
> 
> I must be missing something?

In C++ toInt() returns the value and an error flag via a pointer. In Python
multiple return values are handled using a tuple.

Phil



More information about the PyQt mailing list