Created
November 2, 2013 15:42
-
-
Save rmccue/7280217 to your computer and use it in GitHub Desktop.
Using custom QItemView item widgets with Qt 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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