# This is a sample Python script. from collections import namedtuple from typing import Any from PySide2.QtCore import Qt, QModelIndex, QAbstractTableModel from PySide2.QtWidgets import QApplication, QTableView, QStyledItemDelegate, QLabel PersonRank = namedtuple("PersonRank", ["name", "rank"]) person_list = [] for x in range(1, 10): person_list.append( PersonRank(name=f"name_{x}", rank=x) ) class LabelDelegate(QStyledItemDelegate): def __init__( self, model, parent=None, ): super().__init__(parent=parent) self.model = model def paint(self, painter, option, index): if self.model.data(index, Qt.DisplayRole) == "top 3": label = QLabel("Podium") label.setStyleSheet(f"border-radius: 12px; background-color: gold") else: label = QLabel("Looser") label.setStyleSheet(f"border-radius: 12px; background-color: grey") self.parent().setIndexWidget(index, label) class MyModel(QAbstractTableModel): def __init__( self ): super().__init__() def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> Any: if role == Qt.DisplayRole: if section == 0: return "" elif section == 1: return "name" elif section == 2: return "rank" def data(self, index: QModelIndex, role: int = ...) -> Any: row = index.row() col = index.column() if col == 0: if role == Qt.DisplayRole: text = "top 3" if person_list[row].rank <= 3 else "top 10" return text elif col == 1: if role == Qt.DisplayRole: text = person_list[row].name return text elif col == 2: if role == Qt.DisplayRole: text = person_list[row].rank return text def rowCount(self, parent: QModelIndex = ...) -> int: return len(person_list) def columnCount(self, parent: QModelIndex = ...) -> int: return 3 if __name__ == '__main__': import sys app = QApplication(sys.argv) model = MyModel() tableView = QTableView() tableView.setModel(model) tableView.resizeColumnsToContents() tableView.resize(500, 300) label_delegate = LabelDelegate(model, tableView) tableView.setItemDelegateForColumn(0, label_delegate) tableView.show() sys.exit(app.exec_())