[PyQt] returning numpy numbers in QVariant

Jeremy Sanders jeremy at jeremysanders.net
Fri Apr 23 14:08:39 BST 2010


Phil Thompson wrote:

> Are numpy numbers regular ints/floats or are they a sub-class specific to
> numpy?
> 
> QVariant has been changed (over time) to make sure that no information is
> lost when converting from and to the original object. So an int sub-class
> will stay as a Python object and will not get converted to a C++ int.
> 
> If this is the problem then the fix is to explicitly convert the numpy
> number to a base Python type before converting to a QVariant...
> 
>     return QVariant(float(a[0]))
> 
> ...and this will work for older versions as well.

That workaround works well - thanks.

numpy numbers have their own type (there are actually several types as you 
can choose the precision), so that's probably what causes the QVariant 
problems:

In [1]: import numpy

In [2]: a = numpy.array([1.])

In [3]: type(a[0])
Out[3]: <type 'numpy.float64'>

Jeremy

-- 
http://www.jeremysanders.net/



More information about the PyQt mailing list