QDataWidgetMapper and several delegates

mapper = QtGui.QDataWidgetMapper()
mapper.setModel(my_table_model)
mapper.addMapping(widgetA, 0) #mapping widget to a column
mapper.addMapping(widgetB, 1) #mapping widget to a column
mapper.setItemDelegate(MyDelegateA(widgetA)) #Hmm. Where is the 'column' parameter?
mapper.setItemDelegate(MyDelegateB(widgetB)) #now itemDelegate is rewritten, MyDelegateB will be used

So ... How do I set up multiple delegates for one QDataWidgetMapper? As far as I understand, no. QDataWidgetMapper.setItemDelegateForColumn()Or do I need to create a factory delegate that will use the appropriate delegates?

Thank!

+5
source share
3 answers

You must use one separate delegate and handle the behavior of different widgets in functions setEditorDataand the setModelDatadelegate. For an example (C ++, but directly) check out this article from Qt Quarterly .

+3
source

OK I understood. (At least this works for me). So, the main idea is this class (simplified version), which stores a list of real delegation instances and passes data to \ of them:

class DelegateProxy(QtGui.QStyledItemDelegate):

    def __init__(self, delegates, parent=None):
        QtGui.QStyledItemDelegate.__init__(self, parent)
        self.delegates = delegates

    def setEditorData(self, editor, index):
        delegate = self.delegates[index.column()] 
        delegate.setEditorData(editor, index)

    def setModelData(self, editor, model, index):
        delegate = self.delegates[index.column()]
        delegate.setModelData(editor, model, index)

pastebin

+3

, . QtGui.QDataWidgetMapper, , , addMapping() , dict -, .

- , Qt 4 QAbstractItemView (.. ), ​​ setItemDelegateForColumn(), QDataWidgetMapper .

, , , , , :

mainMapper = QtGui.QDataWidgetMapper()
mainMapper.setModel(my_table_model)

auxMapper1 = QtGui.QDataWidgetMapper()
auxMapper1.setModel(my_table_model)

# If you move the index in the main mapper, the auxiliary will follow
mainMapper.currentIndexChanged.connect(auxMapper1.setCurrentIndex)

mainMapper.addMapping(widgetA, 0) #mapping widget to a column
auxMapper1.addMapping(widgetB, 1) #mapping widget to a column

mainMapper.setItemDelegate(MyDelegateA(widgetA))
auxMapper1.setItemDelegate(MyDelegateB(widgetB))
+1
source

All Articles