PyQt - 如何用sip API 2替换QString

时间:2012-02-02 06:28:46

标签: python pyqt qstring python-sip

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

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

1 个答案:

答案 0 :(得分:2)

切换到QString的v2 api会删除与字符串相关的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
相关问题