pyQt和QTextEdit:为什么会显示一些unicode字符,有些则不显示?

时间:2016-08-19 13:49:34

标签: python unicode pyqt emoji qtextedit

我尝试在pyQt中的QTextEdit中显示unicode文本。

Mac OSX El Capitan上的Python 2.7和PyQt4。

我阅读了一些关于python,QString和unicode的Q& A,并提出了以下运行示例。

运行时,它会向终端输出两个unicode字符串,并在主窗口的QTextEdit中显示它们。 第一个字符串是ok(我在stackoverflow上从Q& A复制它,实际上我不知道它在英语中意味着什么......)。 我看到所有字符都在我的终端和QTextEdit中正确显示。

但是,QTextEdit中缺少第二个字符串的表情符号,尽管它们在终端中正确打印。在QTextEdit中,' ---'之间有两个空格。当我复制QTextEdit中的空白并将它们粘贴到终端中时,我会看到表情符号。所以内容似乎存在,但不是图形表示。

我将字体系列设置为Monaco,因为这是我的文本终端以及Eclipse中的字体,我用它来进行开发。 Eclipse也在其编辑器中正确显示表情符号。

所以我认为摩纳哥字体系列会支持表情符号。

我做错了什么?

感谢您的帮助

阿明

运行示例:很抱歉,这是从现有代码和pyuic生成的ui-class中复制而成的......

# -*- coding: utf-8 -*-
'''
'''
# Importing the necessary Qt classes.
import sys
import re
import sip
import time

from PyQt4 import QtCore
from PyQt4.QtCore import *
from PyQt4.QtGui  import *

from PyQt4 import QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(550, 350)
    self.ExitButton = QtGui.QPushButton(Dialog)
    self.ExitButton.setGeometry(QtCore.QRect(420, 310, 100, 35))
    self.ExitButton.setObjectName(_fromUtf8("ExitButton"))
    self.logView = QtGui.QTextEdit(Dialog)
    self.logView.setGeometry(QtCore.QRect(20, 30, 500, 280))
    self.logView.setReadOnly(False)
    self.logView.setObjectName(_fromUtf8("logView"))

    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
    Dialog.setWindowTitle(_translate("Dialog", "Dialog", None))
    self.ExitButton.setText(_translate("Dialog", "Exit", None))

class MainWindow(QMainWindow, Ui_Dialog):

    # quit
    def finish(self):
        quit()

    def __init__(self):
        QMainWindow.__init__(self)

        # set up User Interface (widgets, layout...)
        self.setupUi(self)

        # custom slots connections
        QtCore.QObject.connect(self.ExitButton, QtCore.SIGNAL("released()"), self.finish) 

        self.logView.setFontFamily("Monaco")

        print("Zażółć gęślą jaźń")
        print("Xc--")

        t = QString.fromUtf8("---Zażółć gęślą jaźń---")
        self.logView.append(t)

        t = QString.fromUtf8("------")
        self.logView.append(t)


        print("family is " + self.logView.fontFamily())
        self.logView.append("family is " + self.logView.fontFamily())


# Main entry to program.  Sets up the main app and create a new window.
def main(argv):

    # create Qt application
    app = QApplication(argv,True)

    # create main window
    wnd = MainWindow() # classname
    wnd.show()

    # Connect signal for app finish
    app.connect(app, QtCore.SIGNAL("lastWindowClosed()"), app, QtCore.SLOT("quit()"))

    # Start the app up
    sys.exit(app.exec_())

if __name__ == "__main__":
    main(sys.argv)

1 个答案:

答案 0 :(得分:1)

你正在使用的python的sys.maxunicode输出是多少?如果它是65535(而不是1114111),那么您使用的是 narrow 版本的python,它不支持BMP之外的字符。

""的unicode代码点是128525,"" 65535是128537,两者都超过了65535.在一个狭窄的版本中,这些将被表示为代理对,大概是Qt不知道如何渲染。

PEP-261开始,可以编译一个广泛的python版本(使用--enable-unicode=ucs4选项),它支持BMP之外的字符。 (对于python> 3.3,只能进行宽版本构建。)

相关问题