PDA

View Full Version : QDataWidgetMapper to synchronize widgets with QTableView



ehamberg
20th February 2008, 23:52
I have always been very happy with Qt's documentation, but when I tried to use QDataWidgetMapper I was a bit disappointed. Is this just supposed to work by magic? It doesn't do that here...

I have a QTableView with a QAbstractTableModel derived vertical model with four columns. This works very nicely and I can view and edit rows in the table view.
I also have a dock widget with some line edits as an alternative way to edit the fields. These fields are the one I have used a QDataWidgetMapper for. The problem is just that it doesn't work. When I select a row and edit it in the dock window, the table is updated, but not the other way around. The fields in the dock widget aren't filled at all when I change the selected row in the table view. *Something* is happening when I change the select row though, because when I submit the text from the dock widget edits the correct row is updated.

The code:

mapper = new QDataWidgetMapper();
mapper->setModel(model);
mapper->addMapping(lineEdit1, 0);
mapper->addMapping(lineEdit2, 1);

connect(view->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)) , mapper, SLOT(setCurrentModelIndex(QModelIndex)));


view is a pointer to the table view, model is a painter to the model I use.

What did I forget to do?

solareclectic
21st February 2008, 05:22
I haven't used this before, but I was just reading the docs today, so I recognized a possible problem...

From this site
http://doc.trolltech.com/4.3/qdatawidgetmapper.html

I see this sample code

QDataWidgetMapper *mapper = new QDataWidgetMapper;
mapper->setModel(model);
mapper->addMapping(mySpinBox, 0);
mapper->addMapping(myLineEdit, 1);
mapper->addMapping(myCountryChooser, 2);
mapper->toFirst();


where I notice a difference on the first line,
but I suspect the problem is not having the last line of this sample code

just a hunch ...
good luck

ehamberg
21st February 2008, 08:32
There are no rows in the model when the programme is started. The user explicitly inserts rows in the table (that is, the model) from the gui.

So there's no «first» row to go to at start.

jpn
21st February 2008, 09:00
Did you call QAbstractItemView::setModel() before that connect statement?

ehamberg
21st February 2008, 21:46
Did you call QAbstractItemView::setModel() before that connect statement?

Yes, the QTableView's model is set to the same as the datawidgetmapper's.