Skip to content

Instantly share code, notes, and snippets.

@rmccue
Created November 2, 2013 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmccue/7280217 to your computer and use it in GitHub Desktop.
Save rmccue/7280217 to your computer and use it in GitHub Desktop.
Using custom QItemView item widgets with Qt 5
class SidebarItemsDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent=None):
super(SidebarItemsDelegate, self).__init__(parent)
self.items = {}
def itemForIndex(self, index):
try:
widget = self.items[index.row()]
except KeyError:
# Use your custom View here!
widget = SidebarItem()
self.items[index.row()] = widget
def paint(self, painter, option, index):
model = index.model()
widget = self.itemForIndex(index)
# Set up the widget's data as needed
# This should actually be in signals, but I'll get there eventually
title = model.data(index, SidebarItemsModel.TitleRole)
details = model.data(index, SidebarItemsModel.DetailRole)
widget.setTitle(title)
widget.setDetails(details)
pixmap = widget.grab()
painter.drawPixmap(option.rect.x(), option.rect.y(), pixmap)
def sizeHint(self, option, index):
widget = self.itemForIndex(index)
return widget.sizeHint()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment