【问题标题】:pyQt and QTextEdit: Why are some unicode characters are shown, others not?pyQt 和 QTextEdit:为什么显示了一些 unicode 字符,而另一些则没有?
【发布时间】:2016-12-26 17:09:01
【问题描述】:

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

它是 Mac OSX El Capitan 上的 Python 2.7 和 PyQt4。

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

运行时,它将两个 unicode 字符串打印到终端,并在其主窗口的 QTextEdit 中显示它们。 第一个字符串没问题(我从stackoverflow上的问答中复制了它,实际上我不知道它在英语中的含义......)。 我看到所有字符在我的终端以及 QTextEdit 中都正确显示。

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

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

所以我假设 Monaco 字体系列会支持表情符号。

我做错了什么?

感谢您的帮助

阿明

运行示例:抱歉篇幅太长,这是从现有代码和 pyuic 生成的 ui 类中逐段复制的...

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

【问题讨论】:

    标签: python unicode pyqt emoji qtextedit


    【解决方案1】:

    sys.maxunicode 对于你正在使用的 python 的输出是什么?如果是 65535(而不是 1114111),则您使用的是 narrow 构建的 python,它不支持 BMP 之外的字符。

    “?”的 unicode 码点是 128525,“?”65535 是 128537,两者都超出了 65535。在狭窄的构建中,这些将表示为代理对,这可能是 Qt 不知道的如何渲染。

    由于PEP-261,可以编译广泛的python 版本(通过使用--enable-unicode=ucs4 选项),它支持BMP 之外的字符。 (对于 python > 3.3,只有宽版本是可能的)。

    【讨论】:

    • 非常感谢。 sys.maxunicode 的输出确实是 65535,所以这解释了我的代码的行为。我将研究如何为我的机器获得广泛的构建......
    • 好的,所以我通过自制软件安装了python3(版本3.5.2),重新安装了sip&pyqt,更新了Eclipse以使用python3并再次尝试。现在sys.maxunicode的输出是1114111,但是运行示例的行为还是一样的,QTextEdit中没有表情,但是还是在Eclipse和终端,还是从QTextEdit复制粘贴两个空格到终端显示表情在终端。我还能尝试什么?
    • @arminphys。您需要安装一个针对广泛的 python 构建编译的 pyqt。
    猜你喜欢
    • 2022-09-28
    • 1970-01-01
    • 2014-11-30
    • 2015-03-30
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    相关资源
    最近更新 更多