Reputation:
I'm using QTableView with QSqlTableModel. In my view I don't display the column containing record id. How can I acquire id of the selected row if its not displayed in any column ?
Thanks for help :)
Upvotes: 7
Views: 4924
Reputation: 8263
Also you can retrieve id directly from QSqlQueryModel but I am not sure it's more convinient then proposed by soulSurfer.
Using QModelIndex
for desired row:
QSqlQueryModel *model = tableView->model();
QSqlRecord record= model->record(desiredIndex->row());
QSqlField field = record.field(id_column_index);
int id = field.value().toInt();
Upvotes: 6
Reputation: 8895
Hmmm...one way is to get the ID from the model and hide it in the view with
void QTableView::setColumnHidden (int column, bool hide)
then you basically get it, but hide it, and from here, you can get it easily with from the model directly by using the index emited from
void QAbstractItemView::activated ( const QModelIndex & index )
Signal.
Upvotes: 5