【发布时间】: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