1

请告诉我如何替换此代码:

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)

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

与 sip API 2 Python 等效。它说“NameError:未定义全局名称'QString'”,因为我改用Python字符串。谢谢你。

[解决了]

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

if unicodedata.category(unichr(self.lastKey)) != 'Cn':
    self.characterSelected.emit(unichr(self.lastKey))
4

1 回答 1

2

切换到 v2 apiQString删除与字符串相关的 Qt 类,以便可以在任何地方使用 python 字符串。

因此,“sip API 2 Python 等价物”只是普通的 python 字符串处理:

>>> 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
于 2012-02-02T21:27:20.767 回答