7

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 :)

2 Answers2

6

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();
beduin
  • 7,943
  • 3
  • 27
  • 24
5

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.

snoofkin
  • 8,725
  • 14
  • 49
  • 86