OP here after a long long time

heres the fix that I used after all, sorry its been a long time, dont remember all the details but it works mostly

Qt Code:
  1. from PyQt5.QtCore import *
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtWidgets import *
  4.  
  5. import sys
  6.  
  7. class My_Model_table(QAbstractTableModel):
  8. def __init__(self, table_data=[], parent=None):
  9. super().__init__()
  10. self.table_data = table_data
  11.  
  12. def rowCount(self, parent):
  13. return len(self.table_data)
  14.  
  15. def columnCount(self, parent):
  16. return 2
  17.  
  18. def data(self, index, role):
  19. if role == Qt.DisplayRole:
  20. value = self.table_data[index.row()]
  21. return value
  22. if role == Qt.TextAlignmentRole:
  23. return Qt.AlignCenter
  24.  
  25.  
  26. class My_table(QTableView):
  27. def __init__(self, parent=None):
  28. super().__init__()
  29. #rowHeight = self.fontMetrics().height()
  30. self.verticalHeader().setDefaultSectionSize(50)
  31.  
  32. def resizeEvent(self, event):
  33. width = event.size().width()
  34. self.setColumnWidth(0, width * 0.80)
  35.  
  36. class HTMLDelegate(QStyledItemDelegate):
  37. def __init__(self, parent=None):
  38. super().__init__()
  39. self.doc = QTextDocument(self)
  40.  
  41. def paint(self, painter, option, index):
  42. painter.save()
  43.  
  44. options = QStyleOptionViewItem(option)
  45.  
  46. self.initStyleOption(options, index)
  47. self.doc.setHtml(options.text)
  48. #options.text = ""
  49.  
  50. style = QApplication.style() if options.widget is None \
  51. else options.widget.style()
  52. style.drawControl(QStyle.CE_ItemViewItem, options, painter)
  53.  
  54. ctx = QAbstractTextDocumentLayout.PaintContext()
  55.  
  56. if option.state & QStyle.State_Selected:
  57. ctx.palette.setColor(QPalette.Text, option.palette.color(
  58. QPalette.Active, QPalette.HighlightedText))
  59. else:
  60. ctx.palette.setColor(QPalette.Text, option.palette.color(
  61. QPalette.Active, QPalette.Text))
  62.  
  63. textRect = style.subElementRect(
  64. QStyle.SE_ItemViewItemText, options)
  65.  
  66. if index.column() != 0:
  67. textRect.adjust(5, 0, 0, 0)
  68.  
  69. thefuckyourshitup_constant = 4
  70. margin = (option.rect.height() - options.fontMetrics.height()) // 2
  71. margin = margin - thefuckyourshitup_constant
  72. textRect.setTop(textRect.top() + margin)
  73.  
  74. painter.translate(textRect.topLeft())
  75. painter.setClipRect(textRect.translated(-textRect.topLeft()))
  76. self.doc.documentLayout().draw(painter, ctx)
  77.  
  78. painter.restore()
  79.  
  80. def sizeHint(self, option, index):
  81. return QSize(self.doc.idealWidth(), self.doc.size().height())
  82.  
  83. if __name__ == '__main__':
  84. app = QApplication(sys.argv)
  85. data = ['1', '2', '<b>3</b>', '4', '5']
  86. main_list = My_table()
  87. main_list.setItemDelegate(HTMLDelegate())
  88. main_list.setModel(My_Model_table(data))
  89. main_list.show()
  90. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode