PDA

View Full Version : why does QSqlTableModel not allow editing?



mikro
17th July 2006, 09:35
hi, i have a tableview showing a QSqlTableModel. I had thought that this would allow me to just click into any cell and change the contents? now i can click in the cell and change, but as soon as i leave the cell the old value is there again. What am i missing?


QSqlTableModel* modDetails;
modDetails->setTable("x"+ka->getShortText());
modDetails->setEditStrategy(QSqlTableModel::OnFieldChange);
modDetails->select();
modDetails->removeColumn(0); // don't show the ID
modDetails->setHeaderData(0, Qt::Horizontal, tr("Abk."));
modDetails->setHeaderData(1, Qt::Horizontal, tr("Bezeichnung"));
// tableDetail is a tableview created in Designer
tableDetail->setModel(modDetails);
tableDetail->show();

wysota
17th July 2006, 09:46
What happens if you change the model manually using setData()? Maybe you provide incorrect values and the database rejects it?

mikro
17th July 2006, 10:40
i will try this later.
but it shouldn't be only this: i have tried with one textfield which allready has text and where i only changed a few letters - that should not make it illegal.

btw: i also don't understand how the user would be able to add a new line? Do i assume right, that i add a pushbutton that triggers this or is there any built-in trick which should work as soon as editing works as well?

Everall
17th July 2006, 13:07
Hello Mikro,

Does it work when you omit the line :
modDetails->removeColumn(0); // don't show the ID

AFAIR removeColumn removes the given column from the model. But it's necessary for the database to keep track of ID 's.

try :
QTableView::setColumnHidden
to hide the column with the ID 's.

Cheers

wysota
17th July 2006, 14:00
btw: i also don't understand how the user would be able to add a new line? Do i assume right, that i add a pushbutton that triggers this or is there any built-in trick which should work as soon as editing works as well?

You have to provide code which operates on the model, you add new items using QSqlTableModel::insertRecord().

mikro
18th July 2006, 22:31
thank you all. i found the problem: the column that i was hiding was an id that was suppossed to autoincrement - unfortunately i had used the sql from MySQL on a SQLite-DB - so what looked like a autoincrement field to me was understood as a normal int by SQLite so it didn't accept the new record without this column.