[Qt-interest] QDataWidgetMapper

Wolfgang Pausch wolfgang.pausch at uibk.ac.at
Tue Oct 6 13:41:55 CEST 2009


Hello, 
 
I am just trying to use the QDataWidgetMapper.  Unfortunately without success.  The problem seems to be that things I enter at the GUI (e.g. in QLineEdits) never find their way into the correspondig model.  Some code snippets, if you need more please ask: 

In the constructor of the widget containing my line edits:
 
 mapping = new QDataWidgetMapper(this); 
 mapping->setModel(mitglied); 
 mapping->setOrientation(Qt::Vertical); 
 mapping->setItemDelegate(new QItemDelegate(mapping));
 
 mitgliedschaftLayout->addWidget(factory->constructVornameLabel(), 2, 0); 
 vornameLineEdit = factory->constructVornameLineEdit(); 
 mitgliedschaftLayout->addWidget(vornameLineEdit, 2, 1); 
 mapping->addMapping(vornameLineEdit, Mitglied::VORNAME); 
 mapping->toFirst(); 

(and several further line edits, combo boxes, etc. as well).  Mitglied is a selfwritten model: Mitglied extends Persistent extends QAbstractListModel. The overwritten methods:
 
 rowCount 
 data() 
 setData() 
 headerData() 
 flags() 
 
setData, headerData, flags never get called.  rowCount and data are called, the latter always with role EDIT_ROLE.  Furthermore, I could verify that the mapping itself is stored, if I query e.g. for the mapping of the vornameLineEdit, I correctly get Mitglied::VORNAME.  The model is defined with the Q_OBJECT macro (is this necessary? at least it should be not harm, shouldn´t it?).

 headerData und flags: 
QVariant Mitglied::headerData(int section, Qt::Orientation orientation, int role) { 
 return QVariant(); // TODO: Maybe generate implementation. 
} 
 
Qt::ItemFlags Mitglied::flags(const QModelIndex &index) { 
 Qt::ItemFlags flags = Persistent::flags(index); 
 flags |= Qt::ItemIsEditable; 
 return flags; 
} 

Now, if I enter some text in my QLineEdit and head on to the next QLineEdit, I would expect setData being called.  But nothing happens, my data never shows up in the model.  

Can anyone give me a clue where my mistake ist?  Or, the other way round, what are the steps one has to do in order to use a QDataWidgetMapper together with a self written model?  Do I have to implement some more methods than the one listed above?  Are there things one easily forgets about?
 
Thanks for all hints!

Wolfgang




More information about the Qt-interest-old mailing list