I would like to add some kind of parent items to a pyqt5 combobox which allow grouping of the items below. the parents should be not selectable and bold if possible, the children a little bit indented.
What I have got so far: I got them bold but I have no idea about the not-selectable option. I could add .setEnabled(False) but this greys them out. also maybe there is a nicer way than adding simply blanks in front of the children items?
from PyQt5.QtWidgets import QWidget, QComboBox, QApplication
import PyQt5.QtGui
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
combo = QComboBox(self)
combo.addItem("option_1")
combo.addItem("group_1")
combo.addItem(" option_2")
combo.addItem(" option_3")
combo.addItem("group_2")
combo.addItem(" option_4")
combo.addItem(" option_5")
font = PyQt5.QtGui.QFont()
font.setBold(True)
item = combo.model().item(1, 0) # group_1 bold
item.setFont(font)
item = combo.model().item(4, 0) # group_2 bold
item.setFont(font)
combo.currentTextChanged.connect(lambda : print(combo.currentText()) )
self.setGeometry(100, 100, 300, 100)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
how current code looks like: