'Modify color and font-size by means of QStyledItemDelegate

I use QtWidgets.QStyledItemDelegate for some rows of a table created by reading a database.

class ItemDelegate(QtWidgets.QStyledItemDelegate):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._values = dict()

    def add_text(self, text, row):
        self._values[row] = text

    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        row = index.row()
        if row in self._values:
            option.text = self._values[row]
            option.displayAlignment = QtCore.Qt.AlignCenter
       ...some code...
       self.delegate = ItemDelegate()
       table.setItemDelegate(self.delegate)

       self.delegate.add_text(self.first_text, 0)
       self.delegate.add_text(self.second_text, 1)
       self.delegate.add_text(self.third_text, 18)

The three texts are positioned correctly but I don't know how to get for self.first_text: color, font-size and font-weight for self.third_text: background If I have to create a "delegate" for each option, how do I then insert it into table.setItemDelegate (self.delegate)?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source