I’m currently developing a desktop app using PyQt. As the titles states I’m trying to create a QListView and populate the list using QWidgets as items.
"""
INPUT: data received from QItemDelegate
OUTPUT: QWidget representation of data
"""
class ListItem(QtGui.QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
self.ui = Ui_ListItem_widget()
self.ui.setupUi(self)
self.Name = self.ui.Name_label
self.icon = self.ui.icon_label
def setData(self, Name, iconDir):
# set label text
# set icon images
"""
Sends data to QWidget and populates it.
INPUT: object
OUTPUT: QWidget populated with object data
"""
class ItemDelegate(QtGui.QItemDelegate):
def __init__(self, parent=None, *args):
QtGui.QItemDelegate.__init__(self, parent, *args)
self._itemWidget = ListItem()
# This is where I get stuck.
# How would I use QItemDelegate to draw a QWidget as a list item?
def paint(self, painter, option, index):
painter.save()
self._itemWidget.setData(#Data to send goes here)
painter.restore()
"""
Model for views.mainwindow._listview
Represents the list as a whole
INPUT: a list of objects
OUTPUT: ListView of objects
"""
class ListModel(QtCore.QAbstractListModel):
def __init__(self, objs=[], parent=None):
QtCore.QAbstractListModel.__init__(self, parent)
self._objs = objs
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
row = index.row()
value = self._objs[row]
return value
def rowCount(self, parent):
return len(self._objs)
I know this can be done with a QListWidget but I would rather use a MVP approach. The main problem I’m having is drawing the widget using the QItemDelegate. When I run my code I dont get errors but nothing show’s on the screen.
I know I have to implement the sizeHint() method and the paint() method but the documentation is confusing when it comes to these two (since they can be used for much more, I find it to be very general)
P.S. I can somewhat understand C++ when it comes to Qt so code examples in C++ are fine.
↧