How to display international scripts in QLabels?

2019-07-13 17:02发布

问题:

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.

What is the correct way to display international (non-alphabetic) scripts in a QLabel?

回答1:

Setting the text of a QLabel to a unicode string (unicode in python2, str in python3) should work fine.

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.

In PyQt4 on python3 QString is gone as now str is already unicode, so just use that.

For instance:

    s = "اردو"
    d = s.decode('utf-8')
    label = QtGui.QLabel(d)
    font = QtGui.QFont("Sheherazade", 40)
    label.setFont(font)