PDA

View Full Version : QSqlTableModel::beforeUpdate has already updated data



JeanC
29th January 2008, 15:12
The EditStrategy on this model has been set to OnManualSubmit (if it was set to OnFieldChange I could at least understand the behaviour).



connect(model, SIGNAL(beforeUpdate(int,QSqlRecord &)), this, SLOT(rowUpdated(int,QSqlRecord &)));
---
void MainWindowImpl::rowUpdated(int row, QSqlRecord &record)
{
QString oldname = model->record(row).value(3).toString(),
newname = record.value(3).toString();
qDebug() << oldname << newname;
}


I get the same 2 new updated strings.
I can work around by storing the old string each time a row gets selected etc but this is not supposed to be working this way or am I wrong?

wysota
29th January 2008, 15:16
From what I understand this method is called before changes in the model are submitted into the database, not when the changes in the editor are submited into the model. With that assumption it works exactly as it should. If you want to have access to the data earlier, either subclass the model and reimplement setData() or subclass the delegate and reimplement setModelData().

JeanC
29th January 2008, 15:51
He wysota,

Thanks for the thorough answer, I looked a while at your suggestions, I understand.

I have the delegate subclassed to do some custom painting but my workaround is simple and working well, so I won't go through the trouble just now.
Subclassing the model just for this seems overkill.

But nice to understand should I need it again, thanks.

Jean.

JeanC
29th January 2008, 16:50
I decided to go for the setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) approach after all.
How do I get to the text from QWidget *editor?

Thanks.


subclass the delegate and reimplement setModelData().

wysota
29th January 2008, 17:31
Cast the editor to the appropriate class using qobject_cast and retrieve the text (like qobject_cast<QLineEdit*>(editor)->text()) or use QObject::property() to fetch the text without casting.

JeanC
29th January 2008, 17:36
Thank you!