Mapping of StandardItemModel via DataWidgetMapper in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData


Overview[edit | edit source]

This will show you the mapping of the StandardItemModel to DataWidgetMapper and directly to QLineEdit.

QDataWidgetMapper can be used to create data-aware widgets by mapping them to sections of an item model. A section is a column of a model if the orientation is horizontal (the default), otherwise a row.

Preconditions[edit | edit source]

Source Code[edit | edit source]

Header File[edit | edit source]

  1. ifndef MODEL_H
  2. define MODEL_H
  1. include <QtGui/QWidget>
  2. include<QLabel>
  3. include<QLineEdit>
  4. include<QPushButton>
  5. include<QStandardItemModel>
  6. include<QStringListModel>
  7. include<QDataWidgetMapper>
  8. include<QGridLayout>
  9. include<QStandardItem>
  10. include<QStringList>

class model : public QWidget {

   Q_OBJECT

public:

   model(QWidget *parent = 0);
   ~model();

private slots:

    void updateButtons(int row);
private:
    void virModel();
    QLabel *nameLabel;
    QLineEdit *nameEdit;
    QPushButton *nextButton;
    QPushButton *previousButton;
    QStandardItemModel *modelversion;
    QStringListModel *typeModel;
    QDataWidgetMapper *mapper;
};
  1. endif // MODEL_H

Source File[edit | edit source]

  1. include "model.h"

model::model(QWidget *parent)

   : QWidget(parent)

{

   virModel();
    nameLabel = new QLabel(tr("Na&me:"),this);
    nameEdit = new QLineEdit(this);
    nameLabel->setBuddy(nameEdit);
    nextButton = new QPushButton(tr("&Next"),this);
    previousButton = new QPushButton(tr("&Previous"),this);
    mapper = new QDataWidgetMapper(this);
    mapper->setModel(modelversion);    //mapping of the dataModel is Done
    mapper->addMapping(nameEdit,0);
    connect(previousButton, SIGNAL(clicked()),mapper,SLOT(toPrevious()));
   connect(nextButton, SIGNAL(clicked()),
            mapper, SLOT(toNext()));
    connect(mapper, SIGNAL(currentIndexChanged(int)),
            this, SLOT(updateButtons(int)));
   QGridLayout *layout = new QGridLayout(this);     //Widget Layout is set
    layout->addWidget(nameLabel, 0, 0, 1, 1);
    layout->addWidget(nameEdit, 0, 1, 1, 1);
    layout->addWidget(previousButton, 0, 2, 1, 1);
    layout->addWidget(nextButton, 1, 2, 1, 1);
    setLayout(layout);
    setWindowTitle(tr("Widget Mapper"));
    mapper->toFirst();

}

model::~model() {

  // No need to delete any object that has got a parent which is properly deleted.

} void model::virModel() //Whole the stringlist items are added to standardItemModel {

   modelversion = new QStandardItemModel(5,1,this);
   QStringList names;
    names << "symbian" << "UIQ" << "ANDROID" << "NOKIA" << "APPLE";
   for (int row = 0; row < 5; ++row)
    {
      QStandardItem *item = new QStandardItem(names[row]);
      modelversion->setItem(row, 0, item);
    }


} void model::updateButtons(int row) //Module Execute when their is updation in QLineEdit {

       previousButton->setEnabled(row > 0);
   nextButton->setEnabled(row < modelversion->rowCount() - 1);

}

Screenshot[edit | edit source]

File:Widmapper.jpg


Related Links[edit | edit source]

Mapping signal via signalMapper

pt:Archived:Mapeamento de StandardItemModel através de QDataWidgetMapper