问题描述
我想在QLabel中显示印度语,阿拉伯语和希伯来语脚本,并指定字体类型.当我尝试将UTF-8编码的字符串传递到QLabel时,脚本无法正确呈现.
I would like to display Indic, Arabic and Hebrew scripts in a QLabel, specifying the font type. When I try to pass a UTF-8 encoded string into a QLabel, the script is not rendered properly.
在QLabel中显示国际(非字母)脚本的正确方法是什么?
What is the correct way to display international (non-alphabetic) scripts in a QLabel?
推荐答案
将QLabel
的文本设置为unicode字符串(python2中为unicode
,python3中为str
)应该可以正常工作.
Setting the text of a QLabel
to a unicode string (unicode
in python2, str
in python3) should work fine.
在python2中,您可以使用QString.fromUtf8
将utf8编码的str
转换为unicode QString
,或将.decode('utf-8')
转换为python unicode
.
In python2 you can use QString.fromUtf8
to convert a utf8-encoded str
to an unicode QString
, or .decode('utf-8')
to a python unicode
.
在python3上的PyQt4中,QString
消失了,因为现在str
已经是unicode了,所以只需使用它即可.
In PyQt4 on python3 QString
is gone as now str
is already unicode, so just use that.
例如:
s = "اردو"
d = s.decode('utf-8')
label = QtGui.QLabel(d)
font = QtGui.QFont("Sheherazade", 40)
label.setFont(font)
这篇关于如何在QLabels中显示国际脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!