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?
Qt Code:
  1. int Dialog::primaryToRow(QAbstractItemModel *model, int primary)
  2. {
  3. if (model == 0)
  4. return -1;
  5. for(int row=0; row < model->rowCount(); ++row)
  6. {
  7. if ( model->index(row, 0).data(Qt::DisplayRole).toInt() == primary )
  8. return row;
  9. }
  10. return -1;
  11. }
  12.  
  13. int Dialog::rowToPrimary(QAbstractItemModel *model, int row)
  14. {
  15. if (model == 0)
  16. return -1;
  17. QModelIndex index = model->index(row,0); //column 0 = primary key
  18. if (!index.isValid())
  19. return -1;
  20. return index.data(Qt::DisplayRole).toInt();
  21. }
To copy to clipboard, switch view to plain text mode 
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