我嘗試在pyQt的QTextEdit中顯示unicode文本。pyQt和QTextEdit:爲什麼顯示一些unicode字符,其他不是?
它是Mac OSX El Capitan上的Python 2.7和PyQt4。
我通讀了一些關於python,QString和unicode的Q &,並提出了以下運行示例。
運行時,它會向終端輸出兩個unicode字符串,並在主窗口的QTextEdit中顯示它們。 第一個字符串是好的(我從Q & A複製它在這裏在stackoverflow,實際上我不知道它是什麼意思在英語...)。 我看到在我的終端以及QTextEdit中正確顯示所有字符。
但是,QTextEdit中缺少第二個字符串的表情符號,儘管它們在終端中正確打印。在QTextEdit中,「---」之間有兩個空格。當我複製QTextEdit中的空白並將它們粘貼到終端中時,我看到了表情符號。所以看起來內容是存在的,但不是圖形表示。
我將字體族設置爲Monaco,因爲這是我在文本終端中以及用於開發的Eclipse中的字體。 Eclipse也在其編輯器中正確顯示錶情符號。
所以我認爲摩納哥字體家族會支持表情符號。
我做錯了什麼?
感謝您的幫助
阿明
運行例如:很抱歉的長度,這在星星點點被複制從現有的代碼和一個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)
非常感謝。 sys.maxunicode的輸出確實是65535,所以這解釋了我的代碼的行爲。我將看看如何獲得我的機器的廣泛構建... – arminphys
好的,所以我通過自制軟件安裝了python3(版本3.5.2),重新安裝了sip&pyqt,更新了Eclipse以使用python3並再次嘗試。現在sys.maxunicode的輸出是1114111,但是運行示例的行爲仍然相同,在QTextEdit中沒有表情符號,但仍然在Eclipse和終端中,仍然複製並粘貼從QTextEdit到終端的兩個空格顯示錶情符號在終端。我還能嘗試什麼? – arminphys
@arminphys。你需要安裝一個已經針對python構建的pyqt。 – ekhumoro