Archived:Mapeamento de StandardItemModel através de QDataWidgetMapper
Aquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Dados do artigo
Testado com
Aparelho(s): S60 Emulator
Compatibilidade
Plataforma(s): S60 3rd Edition, S60 5th Edition
Artigo
Palavras-chave: QStandardItemModel,QDataWidgetMapper
Tradução:
Por valderind4
Última alteração feita por hamishwillee
em 11 Oct 2012
Contents |
Introdução
Este código mostrará o mapeamento do StandardItemModel através de DataWidgetMapper e diretamente para QLineEdit.
QDataWidgetMapper pode ser usado para criar widgets data-aware por intermedio do mapeamento deles, para seções de um modelo de item. Uma seção é uma coluna de um modelo se a orientação é horizontal(padrão), caso contrário é uma linha.
Código fonte
Cabeçalhos de arquivo
#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
Arquivo fonte
#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);
}
Captura de tela



(no comments yet)