PyQt - how to replace QString with sip API 2

2.2k views Asked by At

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))
1

There are 1 answers

7
ekhumoro On BEST ANSWER

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