问题
I am trying to enter a chemical formula using addItem to a QComboBox, but I do not know how to subscript the numbers. I would really appreciate any help I can receive.
I have tried: 1. Using dollar signs ($) before and after the number "$\3$" 2. Using curly brackets and dollar signs "$_{3}$" 3. Using Unicode formatting "\u2083". This one works but it only shows the sub scripted number when you see the options of the drop down menu. After you select the option, it'll display a vertical bar in place of the number (see pics)
self.amp_sub1_Box = QtGui.QComboBox(self)
self.amp_sub1_Box.addItem("H")
self.amp_sub1_Box.addItem("CH\u2083")
self.amp_sub1_Box.addItem("CH\u2082CH\u2083")
回答1:
unicode formatting should work. In this example subscripted numbers are correctly shown in drop down list as well as in selected items:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
from PyQt5 import QtGui, QtWidgets
class MyWidget(QtWidgets.QWidget):
def __init__(self):
QtWidgets.QWidget.__init__(self)
self.setGeometry(200,100,600,700)
self.combo = QtWidgets.QComboBox(self)
self.combo.setGeometry(50,100,300,25)
items = ('CH3-CH3','CH\u2083-CH\u2083','H2O','H\u20820','H2SO4','H\u2082SO\u2084')
for i in range(0,len(items)):
self.combo.addItem(items[i])
app = QtWidgets.QApplication(sys.argv)
widget = MyWidget()
widget.show()
sys.exit(app.exec_())
回答2:
As ekhumoro suggested in the comments, changing the font style solved this problem. Just use: QComboBox.setFont(QtGui.QFont('Verdana'))
来源:https://stackoverflow.com/questions/31522612/how-to-subscript-string-in-qcomboboxs-additem