Search code examples
qtqtsql

Qt QSqlRelationalTableModel can we specify column name instead of column index?


For example, when we're calling QSqlRelationalTableModel::relation(int column), we must specify a column index. If we change the DB model later (e.g. adding a column), things will be broken down. Is it possible to obtain column index by specifying a column name, by which we can do model->setRelation('xxxId', xxx) instead of model->setRelation(2, xxx).


Solution

  • Oh yeah, I missed that there are model->fieldIndex('column_name'), so you can do model->setRelation(model->fieldIndex('column_name'), xxx).