Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDataWidgetMapper to map multiple columns to multiple properties of single custom widget
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 633 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zxpatric
    wrote on last edited by
    #1

    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->setModel(myModel);
    mapper->addMapping(nameLineEdit, 0);
    mapper->addMapping(ageSpinBox, 1);
    Notes:
    
    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.
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      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.

      1 Reply Last reply
      2
      • Z Offline
        Z Offline
        zxpatric
        wrote on last edited by
        #3

        It did work! Thanks!

        Not sure about the penalties but in case anyone is interested

            ```
        
        self.mapper = QDataWidgetMapper(self)
                self.mapper.setModel(model)
                self.mapper.addMapping(self.btnLoad, 7, b'enabled')
        
                self.mapper2 = QDataWidgetMapper(self)
                self.mapper2.setModel(model)
                self.mapper2.addMapping(self.btnLoad, 8, b'text')
        
                self.mapper.toFirst()
                self.mapper2.toFirst()
        
        1 Reply Last reply
        1
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved