[PyQt] QVariant conversion toInt()

Andreas Pakulat apaku at gmx.de
Sun Jul 27 09:20:31 BST 2008


On 26.07.08 19:51:55, lj 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?

Yeap, you're missing the C++ API, which is

int QVariant::toInt( bool* ok = 0 );

which means there's a boolean "in/out" parameter to know wether the
conversion was successful. In Python Phil decided to give back a tuple
that contains the actual integer (in this case) and exactly that boolean
argument to tell you wether the conversion succeeded.

Andreas

-- 
You're at the end of the road again.


More information about the PyQt mailing list