First of all, I'd like to point out the following from https://doc.qt.io/archives/qt-5.15/qitemdelegate.html#details :
We recommend the use of
QStyledItemDelegatewhen creating new delegates.
So, this is what I'm seeing - in the example below, if you set DELEGATE_CHOICE = 0 (i.e. no use of delegate with QComboBox) and run the program, then you get a nice dark blue highlight color when you hover with the mouse pointer over the combo box popup list items:
The same happens if you use DELEGATE_CHOICE = 3 (i.e. you use a subclass of QItemDelegate with the QComboBox)
However, if you set DELEGATE_CHOICE = 1 (i.e. you use a subclass of QStyledItemDelegate with the QComboBox) and run the program, then you get an, also nice, light blue highlight color when you hover with the mouse pointer over the combo box popup list items:
... however, this is not the default color highlight color, and I do not want it here!
So, assuming that I want to follow the documentation's recommendation of using QStyledItemDelegate - can I do that at all in the case of QComboBox (since as the example demonstrates, QStyledItemDelegate corrupts the default highlight colors of QComboBox)? If so, how can I use QStyledItemDelegate with QComboBox without changing the default highlight colors?
Otherwise, is QItemDelegate the right delegate base to use with QComboBox - and if so, how could I have known that? I only realized that after some hours of being puzzled about the color change, when I decided to print out comboBox.itemDelegate(), and subsequently found:
- PyQt - How to set QComboBox in a table view using QItemDelegate
- https://runebook.dev/en/articles/qt/qcombobox/itemDelegate
The example code:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QVBoxLayout, QStyleOptionComboBox, QStyle, QAbstractItemView, QStyledItemDelegate, QStyleOptionButton, QAbstractItemDelegate, QItemDelegate
class SubComboBox(QComboBox):
def __init__(self, parent=None):
super().__init__(parent)
self.setStyleSheet("text-align:left;")
DELEGATE_CHOICE = 1 # 0 # 1 # 2 # 3
if DELEGATE_CHOICE == 1:
class SubDelegate(QStyledItemDelegate): # changes highlight color
pass
elif DELEGATE_CHOICE == 2:
class SubDelegate(QAbstractItemDelegate): # NotImplementedError: QAbstractItemDelegate.sizeHint() is abstract and must be overridden
pass
elif DELEGATE_CHOICE == 3:
class SubDelegate(QItemDelegate): # does not change highlight color; same use in https://stackoverflow.com/q/17615997
pass
class ComboBoxExample(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('ComboBox Example')
self.setGeometry(100, 100, 200, 100)
# Create custom QComboBox
self.comboBox = SubComboBox(self)
#print(f"{self.comboBox.itemDelegate()=}") # PyQt5.QtWidgets.QItemDelegate object
if DELEGATE_CHOICE > 0:
self.comboBox.setItemDelegate(SubDelegate(self))
data = [100, 200, 330, 400, 550]
for num in data:
self.comboBox.addItem(str(num))
# Set up layout
layout = QVBoxLayout()
layout.addWidget(self.comboBox)
layout.addStretch(1) # adds QSpacerItem
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ComboBoxExample()
window.show()
sys.exit(app.exec_())


print(self.comboBox.itemDelegate())). The same is for other complex item-based classes such as QCompleter (when using the popup) or QCalendarWidget (also shown in QDateTimeEdit with the popup enabled). Standard "public" item views (QListView, QTableView, QTreeView and their higher level classes) use QStyledItemDelegate, which, as the name suggests, follows the style. QItemDelegate only uses the palette colors, while styled widgets may consider the palette. So, the real question here is: what do you actually need to do?drawBackground(), since it's not virtual). Yet, being it a simpler class, it usually has better performances with large models.viewOptions()). In any case, I can write a more comprehensive answer if you want, including related resources and source code pointers, as well as possible alternatives to alter the default behavior while trying to keep the expected appearance.