Advertisement
Guest User

Untitled

a guest
May 21st, 2015
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. from PyQt4.QtCore import *
  2. from PyQt4.QtGui import *
  3. import sys
  4.  
  5. class HTMLDelegate(QStyledItemDelegate):
  6. def __init__(self, parent=None):
  7. super(HTMLDelegate, self).__init__(parent)
  8. self.doc = QTextDocument(self)
  9.  
  10. def paint(self, painter, option, index):
  11. painter.save()
  12.  
  13. options = QStyleOptionViewItemV4(option)
  14. self.initStyleOption(options, index)
  15.  
  16. self.doc.setHtml(options.text)
  17. options.text = ""
  18.  
  19. style = QApplication.style() if options.widget is None \
  20. else options.widget.style()
  21. style.drawControl(QStyle.CE_ItemViewItem, options, painter)
  22.  
  23. ctx = QAbstractTextDocumentLayout.PaintContext()
  24.  
  25. if option.state & QStyle.State_Selected:
  26. ctx.palette.setColor(QPalette.Text, option.palette.color(
  27. QPalette.Active, QPalette.HighlightedText))
  28.  
  29. textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
  30. painter.translate(textRect.topLeft())
  31. self.doc.documentLayout().draw(painter, ctx)
  32.  
  33. painter.restore()
  34.  
  35. def sizeHint(self, option, index):
  36. return QSize(self.doc.idealWidth(), self.doc.size().height())
  37.  
  38. if __name__ == '__main__':
  39. app = QApplication(sys.argv)
  40. data = ['1','2','3','4','<b>5</b>','6','7','8','9']
  41. main_list = QListView()
  42. main_list.setItemDelegate(HTMLDelegate())
  43. main_list.setModel(QStringListModel(data))
  44. main_list.show()
  45. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement