×
Namespaces

Variants
Actions

Mapping of StandardItemModel via DataWidgetMapper in Qt

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): S60 Emulator
Compatibility
Platform(s):
Symbian
Article
Keywords: QStandardItemModel,QDataWidgetMapper
Created: mind_freak (26 Jun 2009)
Last edited: hamishwillee (11 Oct 2012)


Contents

Overview

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

Source Code

Header File

#ifndef MODEL_H
#define MODEL_H
 
#include <QtGui/QWidget>
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QStandardItemModel>
#include<QStringListModel>
#include<QDataWidgetMapper>
#include<QGridLayout>
#include<QStandardItem>
#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;
};
 
#endif // MODEL_H

Source File

#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

Widmapper.jpg


Related Links

Mapping signal via signalMapper

This page was last modified on 11 October 2012, at 04:17.
57 page views in the last 30 days.