Valla
Valla

Reputation: 2442

how to use QDataWidgetMapper with QLineEdit

I have a form with three fields and I am trying to populate these fields with values from the model.Below is the code in my header file and the .cpp file:

declarations in myForm.h

private:
    Ui::MyForm *ui;
    QStringListModel *model;
    QDataWidgetMapper *mapper;

Code in myForm.cpp

model=new QStringListModel(this);

QStringList list;
list << "abc"<<"def"<<"ghi";
model->setStringList(list);
mapper=new QDataWidgetMapper();
mapper->setModel(model);
mapper->addMapping(ui->lineEdit_13,0);
mapper->toFirst();

When I run the application the the text field is populated with the first value which is "abc". I am not sure how I can use the QDataWidgetMapper to populate the other two value "def" and "ghi" in the other two text fields on the form.I looked into various options available for QDataWidgetMapper and was not sure how to populate data. It always populates only one value and the other two fields are empty. How can all the three values be read from the model or am I doing anything wrong here?I just started using models in my Qt application.

Upvotes: 2

Views: 1733

Answers (1)

Leiaz
Leiaz

Reputation: 2917

Your model have one column and three rows. If you want to treat the column as one "item", you have to indicate the orientation to QDataWidgetMapper with QDataWidgetMapper::setOrientation(Qt::Orientation). The default is Qt::Horizontal (one "item" is one row of the model), you want to set it to Qt::Vertical.

Then you use addMapping like you did, each row is a section :

mapper->addMapping(ui->lineEdit_abc,0);
mapper->addMapping(ui->lineEdit_def,1);
mapper->addMapping(ui->lineEdit_ghi,2);

Upvotes: 2

Related Questions