Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- import sys
- class HTMLDelegate(QStyledItemDelegate):
- def __init__(self, parent=None):
- super(HTMLDelegate, self).__init__(parent)
- self.doc = QTextDocument(self)
- def paint(self, painter, option, index):
- painter.save()
- options = QStyleOptionViewItemV4(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))
- textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
- painter.translate(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','3','4','<b>5</b>','6','7','8','9']
- main_list = QListView()
- main_list.setItemDelegate(HTMLDelegate())
- main_list.setModel(QStringListModel(data))
- main_list.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement