QTableView + QSqlTableModel - how to read the identifier of the selected row

I am using QTableView with QSqlTableModel. In my opinion, I am not showing a column containing the record identifier. How can I get the id of the selected row if it does not appear in any column?

Thanks for the help:)

+6
source share
2 answers

You can also get the identifier directly from QSqlQueryModel, but I'm not sure if it is more convenient than suggested by soulSurfer.

Using QModelIndex for the desired line:

 QSqlQueryModel *model = tableView->model(); QSqlRecord record= model->record(desiredIndex->row()); QSqlField field = record.field(id_column_index); int id = field.value().toInt(); 
+5
source

Hmmm ... one way is to get the identifier from the model and hide it in the view using

 void QTableView::setColumnHidden (int column, bool hide) 

then you basically get it, but hide it, and from here you can easily get it using the model, using the index emitted from

 void QAbstractItemView::activated ( const QModelIndex & index ) 

Signal.

+4
source

Source: https://habr.com/ru/post/886555/


All Articles