1

Please show me how to replace this code:

import sip
sip.setapi("QString", 2)
...

text = QString.fromLatin1("<p>Character: <span style=\"font-size: 16pt; font-family: %1\">").arg(self.displayFont.family()) + \
            QChar(key) + \
            QString.fromLatin1("</span><p>Value: 0x") + \
            QString.number(key, 16)

and

if QChar(self.lastKey).category() != QChar.NoCategory:
    self.characterSelected.emit(QString(QChar(self.lastKey)))

with sip API 2 Python equivalent. It says "NameError: global name 'QString' is not defined" because I use Python strings instead. Thank you.

[SOLVED]

text = ('<p>Character: <span style="font-size: 16pt; font-family: %s">%s</span>
    <p>Value: %#x' % (self.displayFont.family(), unichr(key), key))

and

if unicodedata.category(unichr(self.lastKey)) != 'Cn':
    self.characterSelected.emit(unichr(self.lastKey))
linuxoid
  • 1,415
  • 3
  • 14
  • 32

1 Answers1

2

Switching to the v2 api for QString removes the string-related Qt classes so that python strings can be used everywhere instead.

The "sip API 2 Python equivalent" is therefore just normal python string-handling:

>>> text = ('<p>Character: <span style="font-size: 16pt; '
...         'font-family: %s">%s</span><p>Value: %#x' %
...         (font.family(), unichr(key), key))
>>> 
>>> print text
<p>Character: <span style="font-size: 16pt; font-family: Sans Serif">A</span><p>Value: 0x41
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thank you. It says "NameError: global name 'font' is not defined". What module is the font in? – linuxoid Feb 02 '12 at 23:43
  • @user665327. `font` is just a generic name I used for the purposes of testing. Obviously you will have to replace that with, e.g. `self.displayFont` (as defined in your own example). Just as a matter of curiosity: why are you using `sip.setapi`? Is it your own code? And what version of python are you using? – ekhumoro Feb 02 '12 at 23:53
  • Thank you, that worked. I use API 2 because I don't want to convert all QStrings to Python strings as some modules don't accept QStrings, SQLAlchemy for example (see here http://stackoverflow.com/questions/8876269/pyqt-sqlalchemy-doesnt-accept-qstring). That particular code is Trolltech's example for inserting a symbol in an editor. I use Python 2.7. – linuxoid Feb 03 '12 at 00:59
  • Now QChar needs to be replaced as well. What would be Python equivalent for QChar.NoCategory (I added some code above)? – linuxoid Feb 03 '12 at 01:06
  • @user665327. The equivalent functionality in python is provided by [unicodedata.category](http://docs.python.org/library/unicodedata.html#unicodedata.category), which returns values given in the [unicode character database](http://www.unicode.org/Public/5.1.0/ucd/UCD.html#General_Category_Values). The value `QChar.NoCategory` seems to correspond with `Cn`. – ekhumoro Feb 03 '12 at 01:29
  • I added import unicodedata and I have this: if unicodedata.category(unichr(self.lastKey)) != 'Cn': self.characterSelected.emit(str(unichr(self.lastKey))) but now it says "UnicodeEncodeError: 'ascii' codec can't encode character u'\u2082' in position 0: ordinal not in range(128)" – linuxoid Feb 03 '12 at 02:15
  • I got it (see above). Thank you for your help. – linuxoid Feb 03 '12 at 03:14