Advertisement
Guest User

with icons

a guest
May 11th, 2015
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 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.         self.doc.setDocumentMargin(0)
  10.  
  11.     def paint(self, painter, option, index):
  12.         painter.save()
  13.  
  14.         options = QStyleOptionViewItemV4(option)
  15.         self.initStyleOption(options, index)
  16.  
  17.         self.doc.setHtml(options.text)
  18.         options.text = ""
  19.  
  20.         style = QApplication.style() if options.widget is None \
  21.             else options.widget.style()
  22.         style.drawControl(QStyle.CE_ItemViewItem, options, painter)
  23.  
  24.         ctx = QAbstractTextDocumentLayout.PaintContext()
  25.  
  26.         if option.state & QStyle.State_Selected:
  27.             ctx.palette.setColor(QPalette.Text, option.palette.color(
  28.                                  QPalette.Active, QPalette.HighlightedText))
  29.  
  30.         textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
  31.         painter.translate(textRect.topLeft())
  32.         self.doc.documentLayout().draw(painter, ctx)
  33.  
  34.         painter.restore()
  35.  
  36. if __name__ == '__main__':
  37.     app = QApplication(sys.argv)
  38.     data = ['1','2','3','4','5','6','7','8','9']
  39.     main_list = QListView()
  40.     model = QStandardItemModel()
  41.     file_icon = main_list.style().standardIcon(QStyle.SP_FileIcon)
  42.  
  43.     for n in data:
  44.         item = QStandardItem(n)
  45.         item.setIcon(file_icon)
  46.         model.appendRow(item)
  47.  
  48.     main_list.setItemDelegate(HTMLDelegate())
  49.     main_list.setModel(model)
  50.     main_list.show()
  51.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement