Smarter delegates

I recently dove into QItemDelegates and realized how easy it can be to make delegates without resorting to subclassing QItemDelegate itself.
I wrapped the functionality in a neat little function, just pass an editor that has a value() getter and a setValue() setter and you should be good to go

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
_typedDelegates = {}


def makeDelegate(cls):
    if cls in _typedDelegates:
        return _typedDelegates[cls]
    d = QStyledItemDelegate()
    _typedDelegates[cls] = d
    f = QItemEditorFactory()
    d.setItemEditorFactory(f)

    class EditorWrapper(QItemEditorCreatorBase):
        def __init__(self, cls):
            super(EditorWrapper, self).__init__()
            self.cls = cls

        def createWidget(self, parent):
            return self.cls(parent)

        def valuePropertyName(self):
            return 'value'

    f.registerEditor(0, EditorWrapper(cls))
    return d

Leave a Reply

Your email address will not be published. Required fields are marked *