how do I find a primary key using a QSqlRelationalTableModel?
-
@jdent said in how do I find a primary key using a QSqlRelationalTableModel?:
how can I do it?
By looking in the documentation and finding QSqlTableModel::primaryKey()
-
@Christian-Ehrlicher No I wasn't clear. I know what the primary key of a table is. I need to find a record in that table with primary key == 5, for instance, using the model QSqlRelationalTableModel.
I can always do a loop:int located_row = -1; for( int row =0; row < model->rowCount(); ++row) { auto id = model->record(row).value(0).toInt(); if(id == 2) { located_row = row; break; } }
but there must be a method!
-
@jdent
You have to iterate through the rows looking for the value you want.
If you use aQSortFilterProxyModel
you can take advantage of binary search for speed if you sort by primary key.QSqlRelationalTableModel
is not important here, all that gives is a "lookup" on one field to map e.g. an integer value to a corresponding string from a related table for display.