from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
def __init__(self, table_data=[], parent=None):
super().__init__()
self.table_data = table_data
def rowCount(self, parent):
return len(self.table_data)
def columnCount(self, parent):
return 2
def data(self, index, role):
if role == Qt.DisplayRole:
value = self.table_data[index.row()]
return value
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
def __init__(self, parent=None):
super().__init__()
#rowHeight = self.fontMetrics().height()
self.verticalHeader().setDefaultSectionSize(50)
def resizeEvent(self, event):
width = event.size().width()
self.setColumnWidth(0, width * 0.80)
class HTMLDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super().__init__()
def paint(self, painter, option, index):
painter.save()
self.initStyleOption(options, index)
self.doc.setHtml(options.text)
#options.text = ""
else options.widget.style()
style.
drawControl(QStyle.
CE_ItemViewItem, options, painter
)
if option.
state & QStyle.
State_Selected: ctx.
palette.
setColor(QPalette.
Text, option.
palette.
color( else:
ctx.
palette.
setColor(QPalette.
Text, option.
palette.
color(
textRect = style.subElementRect(
QStyle.
SE_ItemViewItemText, options
)
if index.column() != 0:
textRect.adjust(5, 0, 0, 0)
thefuckyourshitup_constant = 4
margin = (option.rect.height() - options.fontMetrics.height()) // 2
margin = margin - thefuckyourshitup_constant
textRect.setTop(textRect.top() + margin)
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
def sizeHint(self, option, index):
return QSize(self.
doc.
idealWidth(), self.
doc.
size().
height())
if __name__ == '__main__':
data = ['1', '2', '<b>3</b>', '4', '5']
main_list = My_table()
main_list.setItemDelegate(HTMLDelegate())
main_list.setModel(My_Model_table(data))
main_list.show()
sys.exit(app.exec_())
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class My_Model_table(QAbstractTableModel):
def __init__(self, table_data=[], parent=None):
super().__init__()
self.table_data = table_data
def rowCount(self, parent):
return len(self.table_data)
def columnCount(self, parent):
return 2
def data(self, index, role):
if role == Qt.DisplayRole:
value = self.table_data[index.row()]
return value
if role == Qt.TextAlignmentRole:
return Qt.AlignCenter
class My_table(QTableView):
def __init__(self, parent=None):
super().__init__()
#rowHeight = self.fontMetrics().height()
self.verticalHeader().setDefaultSectionSize(50)
def resizeEvent(self, event):
width = event.size().width()
self.setColumnWidth(0, width * 0.80)
class HTMLDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super().__init__()
self.doc = QTextDocument(self)
def paint(self, painter, option, index):
painter.save()
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)
self.doc.setHtml(options.text)
#options.text = ""
style = QApplication.style() if options.widget is None \
else options.widget.style()
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.HighlightedText))
else:
ctx.palette.setColor(QPalette.Text, option.palette.color(
QPalette.Active, QPalette.Text))
textRect = style.subElementRect(
QStyle.SE_ItemViewItemText, options)
if index.column() != 0:
textRect.adjust(5, 0, 0, 0)
thefuckyourshitup_constant = 4
margin = (option.rect.height() - options.fontMetrics.height()) // 2
margin = margin - thefuckyourshitup_constant
textRect.setTop(textRect.top() + margin)
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
self.doc.documentLayout().draw(painter, ctx)
painter.restore()
def sizeHint(self, option, index):
return QSize(self.doc.idealWidth(), self.doc.size().height())
if __name__ == '__main__':
app = QApplication(sys.argv)
data = ['1', '2', '<b>3</b>', '4', '5']
main_list = My_table()
main_list.setItemDelegate(HTMLDelegate())
main_list.setModel(My_Model_table(data))
main_list.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks