Search code examples
pythonpyqtqcombobox

Using superscript in QComboBox


I used a similar approach in this post to add superscripts to some items in my combobox. But using the unicode and setting the font doesn't work.

Here is the code I used:

# Define squared units
units = ['m\u00B2', 'km\u00B2']
self.dockwidget.comboBox.clear()
self.dockwidget.comboBox.addItems(oppMap_units)
self.dockwidget.comboBox.setFont(QtGui.QFont('Verdana'))

Example


My Qt version is 4.8.5


Solution

  • If you're using Python 2, you must remember to use unicode:

    units = [u'm\u00B2', u'km\u00B2']
    

    Otherwise, the strings will be decoded as ascii.