Important: Please read the Qt Code of Conduct -

QDataWidgetMapper to map multiple columns to multiple properties of single custom widget

  • Understand that currently there is one-one limitation to QDataWidgetMapper::addMapping but any workaround would also be appreciated. Thanks!

    For example, there are needs of mapping to both the enabled and text property of a QPushButton but only the latter one (text) will take effect if I did following.

        self.mapper.addMapping(self.btnLoad, 7, b'enabled')
        self.mapper.addMapping(self.btnLoad, 8, b'text')
    void QDataWidgetMapper::addMapping(QWidget *widget, int section)
    Adds a mapping between a widget and a section from the model. The section is a column in the model if the orientation is horizontal (the default), otherwise a row.
    For the following example, we assume a model myModel that has two columns: the first one contains the names of people in a group, and the second column contains their ages. The first column is mapped to the QLineEdit nameLineEdit, and the second is mapped to the QSpinBox ageSpinBox:
    QDataWidgetMapper *mapper = new QDataWidgetMapper;
    mapper->addMapping(nameLineEdit, 0);
    mapper->addMapping(ageSpinBox, 1);
    If the widget is already mapped to a section, the old mapping will be replaced by the new one.
    Only one-to-one mappings between sections and widgets are allowed. It is not possible to map a single section to multiple widgets, or to map a single widget to multiple sections.

  • Lifetime Qt Champion

    Im just thinking out loud, but did you try with 2 QDataWidgetMapper for same model ?
    I dont know if that is ok/works but since QDataWidgetMapper keeps the "bindings"
    it might work.

  • It did work! Thanks!

    Not sure about the penalties but in case anyone is interested

    self.mapper = QDataWidgetMapper(self)
            self.mapper.addMapping(self.btnLoad, 7, b'enabled')
            self.mapper2 = QDataWidgetMapper(self)
            self.mapper2.addMapping(self.btnLoad, 8, b'text')

  • Lifetime Qt Champion


    There's currently no possible workaround with a single QDataWidgetMapper. The internal logic does not handle multiple "same source widget".

Log in to reply