Изменение цвета текста ячейки в table widget pyqt5

Не поможете сделать функцию для изменения цвета текста определенной ячейки, указанную в координатах (1,3) в table widget pyqt5?


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

Автор решения: S. Nick

Класс QStyledItemDelegate предоставляет средства отображения и редактирования элементов данных из модели.

from PyQt5.QtWidgets import QApplication, QStyledItemDelegate, QTableWidget 
from PyQt5.QtGui import QColor, QPalette, QFont


class ColorDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        if index.row() == 3 and index.column() == 1 :
            option.palette.setColor(QPalette.Text, QColor("red"))
        else:
            option.palette.setColor(QPalette.Text, QColor("#2D2424"))
        QStyledItemDelegate.paint(self, painter, option, index)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    app.setFont(QFont("Times", 12, QFont.Bold))
    w = QTableWidget(5, 5)
    w.setHorizontalHeaderLabels(['Column0','Column1','Column2','Column3','Column4'])
    w.setVerticalHeaderLabels(['Row0','Row1','Row2','Row3','Row4'])
    w.setItemDelegate(ColorDelegate())
    w.resize(600, 300)
    w.show()
    sys.exit(app.exec_())

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

→ Ссылка