- Qt 6.11
- Qt Widgets
- Item Views Examples
- Combo Widget Mapper Example
- Qt 6.11.1 Reference Documentation

# Combo Widget Mapper Example

The Combo Widget Mapper example shows how to use a QDataWidgetMapper to map information from a model to specific widgets on a form.

We create a `Window` class with an almost identical user interface, except that, instead of providing a spin box so that each person's age can be entered, we provide a combo box to allow their addresses to be classified as "Home", "Work" or "Other".

#### Person Class Definition

We define a struct `Person` containing name, address and address type to represent the model data:

     struct Person
     {
     private:
         Q_GADGET
         Q_PROPERTY(QString name MEMBER name)
         Q_PROPERTY(QString address MEMBER address)
         Q_PROPERTY(QString type MEMBER type)
     public:

         QString name;
         QString address;
         QString type;
     };

It is declared to be a Q_GADGET with the members to be properties of the same name to be able to conveniently populate a QRangeModel.

#### Window Class Definition

The class provides a constructor and a slot to keep the buttons up to date:

     class Window : public QWidget
     {
         Q_OBJECT

     public:
         Window(QWidget *parent = nullptr);

     private slots:
         void updateButtons(int row);

     private:
         QLineEdit *nameEdit;
         QTextEdit *addressEdit;
         QComboBox *typeComboBox;
         QPushButton *nextButton;
         QPushButton *previousButton;
         QList<Person> data;
         QRangeModel *model;
         QDataWidgetMapper *mapper;
     };

In addition to the QDataWidgetMapper object and the controls used to make up the user interface, we use a QList<Person> to hold our data and a QRangeModel operating on it.

#### Window Class Implementation

The constructor of the `Window` class can be explained in several parts. In the first part, we set up the widgets used for the user interface:

     Window::Window(QWidget *parent)
         : QWidget(parent),
           nameEdit(new QLineEdit),
           addressEdit(new QTextEdit),
           typeComboBox(new QComboBox),
           nextButton(new QPushButton(tr("&Next"))),
           previousButton(new QPushButton(tr("&Previous"))),

The person list is populated using an initalizer list, which is then passed to the QRangeModel:

           data{Person{u"Alice"_s,  u"<qt>123 Main Street<br/>Market Town</qt>"_s,                      u"0"_s},
                Person{u"Bob"_s,    u"<qt>PO Box 32<br/>Mail Handling Service<br/>Service City</qt>"_s, u"1"_s},
                Person{u"Carol"_s,  u"<qt>The Lighthouse<br/>Remote Island</qt>"_s,                     u"2"_s},
                Person{u"Donald"_s, u"<qt>47338 Park Avenue<br/>Big City</qt>"_s,                       u"0"_s},
                Person{u"Emma"_s,   u"<qt>Research Station<br/>Base Camp<br/>Big Mountain</qt>"_s,      u"2"_s}},
           model(new QRangeModel(data, this)),
           mapper(new QDataWidgetMapper(this))

Note that we set up the mapping the combo box in the same way as for other widgets, but that we apply its own model holding information about the types of address to it, so that it will display the address type string rather than the data from the model containing data about each person:

         typeComboBox->setModel(new QStringListModel({ tr("Home"), tr("Work"), tr("Other") }, this));

Next, we set up the widget mapper, relating each input widget to a column in the model specified by the call to setModel():

         mapper->setModel(model);
         mapper->addMapping(nameEdit, 0);
         mapper->addMapping(addressEdit, 1);
         mapper->addMapping(typeComboBox, 2, "currentIndex");

For the combo box, we pass an extra argument to tell the widget mapper which property to relate to values from the model. As a result, the user is able to select an item from the combo box, and the corresponding value stored in the widget's `currentIndex` property will be stored in the model.

The rest of the constructor sets up connections and layouts:

         connect(previousButton, &QAbstractButton::clicked,
                 mapper, &QDataWidgetMapper::toPrevious);
         connect(nextButton, &QAbstractButton::clicked,
                 mapper, &QDataWidgetMapper::toNext);
         connect(mapper, &QDataWidgetMapper::currentIndexChanged,
                 this, &Window::updateButtons);

         auto *formLayout = new QFormLayout;
         formLayout->addRow(tr("Na&me:"), nameEdit);
         formLayout->addRow(tr("&Address:"), addressEdit);
         formLayout->addRow(tr("&Type:"), typeComboBox);

         auto *buttonLayout = new QVBoxLayout;
         buttonLayout->addWidget(previousButton);
         buttonLayout->addWidget(nextButton);
         buttonLayout->addStretch();

         auto *mainLayout = new QHBoxLayout(this);
         mainLayout->addLayout(formLayout);
         mainLayout->addLayout(buttonLayout);

         setWindowTitle(tr("Delegate Widget Mapper"));
         mapper->toFirst();
     }

We show the implementation of the `updateButtons()` slot for completeness:

     void Window::updateButtons(int row)
     {
         previousButton->setEnabled(row > 0);
         nextButton->setEnabled(row < model->rowCount() - 1);
     }

#### Summary and Further Reading

The use of a separate model for the combo box provides a menu of choices that are separate from the data stored in the main model. Using a named mapping that relates the combo box's `currentIndex` property to a column in the model effectively allows us to store a look-up value in the model.

However, when reading the model outside the context of the widget mapper, we need to know about the `typeModel` to make sense of these look-up values. It would be useful to be able to store both the data and the choices held by the `typeModel` in one place. This is covered by the SQL Widget Mapper Example.

Example project @ code.qt.io