Advertisement
Guest User

Untitled

a guest
Feb 15th, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. class HTMLDelegate(Qw.QStyledItemDelegate):
  2.         def __init__(self, parent=None):
  3.             super().__init__()
  4.             self.doc = Qg.QTextDocument(self)
  5.  
  6.         def paint(self, painter, option, index):
  7.             painter.save()
  8.  
  9.             options = Qw.QStyleOptionViewItem(option)
  10.  
  11.             self.initStyleOption(options, index)
  12.             self.doc.setHtml(options.text)
  13.             options.text = ""
  14.  
  15.             style = Qg.QApplication.style() if options.widget is None \
  16.                 else options.widget.style()
  17.             style.drawControl(Qw.QStyle.CE_ItemViewItem, options, painter)
  18.  
  19.             ctx = Qg.QAbstractTextDocumentLayout.PaintContext()
  20.  
  21.             if option.state & Qw.QStyle.State_Selected:
  22.                 ctx.palette.setColor(Qg.QPalette.Text, option.palette.color(
  23.                     Qg.QPalette.Active, Qg.QPalette.HighlightedText))
  24.             else:
  25.                 ctx.palette.setColor(Qg.QPalette.Text, option.palette.color(
  26.                     Qg.QPalette.Active, Qg.QPalette.Text))
  27.  
  28.             textRect = style.subElementRect(
  29.                 Qw.QStyle.SE_ItemViewItemText, options)
  30.  
  31.             if index.column() != 0:
  32.                 textRect.adjust(5, 0, 0, 0)
  33.  
  34.             thefuckyourshitup_constant = 4
  35.             margin = (option.rect.height() - options.fontMetrics.height()) // 2
  36.             margin = margin - thefuckyourshitup_constant
  37.             textRect.setTop(textRect.top() + margin)
  38.  
  39.             painter.translate(textRect.topLeft())
  40.             painter.setClipRect(textRect.translated(-textRect.topLeft()))
  41.             self.doc.documentLayout().draw(painter, ctx)
  42.  
  43.             painter.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement