[PyQt] unicode(QChar) produces unexpected result?

Phil Thompson phil at riverbankcomputing.com
Sat Aug 11 11:28:43 BST 2012


On Thu, 9 Aug 2012 13:15:10 -0700, David Cortesi <davecortesi at gmail.com>
wrote:
> To get a python string copy of the contents of a QString object, I have
> been using unicode(some_qstring) .
> 
>>>> qs = QString(u'BAZ')
>>>> len(unicode(qs))
> 3
> 
> So I was surprised when I did the analogous thing to get a
> python string copy of the contents of a QChar.
> 
>>>> qc = QChar(u'Z')
>>>> len(unicode(qc))
> 24
>>>> unicode(qc)
> u'PyQt4.QtCore.QChar(0x5a)'
> 
> It appears to produce the _repr_ value. Is this intended?

It's a side effect of not implementing __unicode__ for QChar. Now fixed.

> Investigating I find that QChar has a unicode() method which
> assistant says returns a ushort.
> 
>>>> qc.unicode()
> 90L
> 
> note the L suffix - is this an artifact of the cpp interface code or
what?

SIP converts all unsigned values to Python long objects. Now fixed so that
shorts and bytes get converted to int objects.

> In the end this is the only way I see to make a QChar into a python
string:
> 
>>>> unicode(QString(qc))
> u'Z'
> 
> Thoughts?

You can now do unicode(qc) as you would expect.

Thanks,
Phil


More information about the PyQt mailing list