Как задать стиль выбранного элемента QComboBox в PyQt5?

Как добавить какой-то стиль для выбранного элемента (в данном случае для 1)?

Например как закрасить фон выбранного элемента в красный?

введите сюда описание изображения

Вот код

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow
import sys


class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.comboBox = QtWidgets.QComboBox(self)
        self.comboBox.addItem("1")
        self.comboBox.addItem("2")
        self.comboBox.addItem("3")
        self.comboBox.setStyleSheet('''''')


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = Ui_MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

Ответы (1 шт):

Автор решения: MIkhail
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QBrush, QColor
import sys


class SelectDelegate(QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        if self.parent().currentIndex() == index.row():
            option.backgroundBrush = QColor(Qt.red)

class Ui_MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.comboBox = QComboBox(self)
        self.comboBox.setItemDelegate(SelectDelegate(self.comboBox))

        self.comboBox.addItem("1")
        self.comboBox.addItem("2")
        self.comboBox.addItem("3")


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = Ui_MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

введите сюда описание изображения

→ Ссылка