QSqlTableModel: primary key to model row
Hello,
I have a model (QSqlTableModel), loaded with a table like this:
"CREATE TABLE person (id INTEGER PRIMARY KEY, name VARCHAR(20))"
How can I get the row number of the model from the table primary key?
Is there a better way than looping thru all rows of the model?
Code:
{
if (model == 0)
return -1;
for(int row=0; row < model->rowCount(); ++row)
{
if ( model->index(row, 0).data(Qt::DisplayRole).toInt() == primary )
return row;
}
return -1;
}
{
if (model == 0)
return -1;
QModelIndex index
= model
->index
(row,
0);
//column 0 = primary key if (!index.isValid())
return -1;
return index.data(Qt::DisplayRole).toInt();
}
I cannot modify the model, so "model->filter" is no good.
My loop code works, but for a 10K rows table it is too heavy (I've a custom view which calls it twice for every row).
Any better solution? Perhaps a list "QMap<int, int>" ?
Thank you,
Nero