How to update column B of a QSqlRelationalTableModel after the update of column A?
-
I have two columns in a model (QSqlRelationalTableModel).
I want after the update of column A (in a QTableView), another column, column B, to be automatically updated.
How can I do this?
I suppose I must subclass the QSqlRelationalTableModel?
Any info is welcome. -
Or do it in your db directly with an update trigger.
-
The problem is that with the trigger (I tried it) I cannot make the text in the second column have lower case (non latin characters) (it's an SQLite db, does not support lower case for non latin).
(I 've already seen this) -
@Panoss
Then in yoursetData()
override update column B whenever you see column A being updated.@JonB said in How to update column B of a QSqlRelationalTableModel after the update of column A?:
@Panoss
Then in yoursetData()
override update column B whenever you see column A being updated.This is what I 've done so far:
bool ArticlesModel::setData(const QModelIndex &index, const QVariant &value, int role){ int nameIndex = 1; // index of field 'name' if(index.column() == nameIndex){ ///index.model(). ????? } return QSqlTableModel::setData(index, value, role); }
So how do I update column 3?
-
@JonB said in How to update column B of a QSqlRelationalTableModel after the update of column A?:
@Panoss
Then in yoursetData()
override update column B whenever you see column A being updated.This is what I 've done so far:
bool ArticlesModel::setData(const QModelIndex &index, const QVariant &value, int role){ int nameIndex = 1; // index of field 'name' if(index.column() == nameIndex){ ///index.model(). ????? } return QSqlTableModel::setData(index, value, role); }
So how do I update column 3?
This is what I 've done and seems to work:
bool ArticlesModel::setData(const QModelIndex &index, const QVariant &value, int role){ int nameColumnIndex = 1; int name_lowerColumnIndex = 3; if(index.column() == nameColumnIndex){ int row = index.row(); QModelIndex name_lower_index = index.model()->index(row,name_lowerColumnIndex); QString v = value.toString(); ArticlesModel::setData(name_lower_index, v.toLower(), Qt::EditRole); } return QSqlTableModel::setData(index, value, role); }
Is it ok?