[PyQt] returning SQL result as ordinary python string?

Andrew Suffield asuffield at suffields.me.uk
Thu May 3 15:02:33 BST 2012


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)


More information about the PyQt mailing list