[PyQt] returning SQL result as ordinary python string?

Linos info at linos.es
Thu May 3 16:43:03 BST 2012


El 03/05/12 16:02, Andrew Suffield escribió:
> On Thu, May 03, 2012 at 08:15:26AM -0400, Mark Mordeca wrote:
>> while(query.next()):
>>
>>      idString=str(query.value(0).toString())
>>
>>      passwordString=str(query.value(1).toString())
>>
>>
>>
>> query.value()  returns a QVariant, so you use .toString() to convert it to
>> a QString, then use str to convert the QString to a python string.
> 
> One step less, and more sensible behaviour:
> 
> query.value(0).toPyObject()
> 
> It'll come out as the python native type for the variant, so QString
> goes to unicode, etc. Also works for random python classes that
> QVariant doesn't know about.
> 
> (This should be better documented)
> _______________________________________________

I think that this is only True if you are using sip.setapi("QString", 2) if you
are using api 1 you get this:

In [2]: QVariant("test").toPyObject()
Out[2]: PyQt4.QtCore.QString(u'test')

So you need to explicit conversion to unicode anyway.

Regards,
Miguel Angel.


More information about the PyQt mailing list