I have a model, deriving from QStandardItemModel, that is used to represent keys with associated values. The key is stored in the first column of each row, while the associated values are stored in the following columns from each row.

I have quite often to verify if a certain key is already stored in the model, if this is the case I am leaving the model untouched otherwhise I am adding a new row.

So each time I have to verify if a certain key is already stored in the model, I am parsing the entire model like this:

Qt Code:
  1. // frame_id contains the key we may want to store in the model
  2.  
  3. // Get the amount of rows already stored in the model
  4. int rowCount = m_model->rowCount(QModelIndex());
  5. int row;
  6.  
  7. unsigned int stored_id;
  8. // Parse the model to look if this identifier is already stored
  9. for (row = 0; row < rowCount; row++) {
  10. stored_id = m_model->data(m_model->index(row,0,QModelIndex()),Qt::DisplayRole).toUInt();
  11. if (stored_id == frame_id) {
  12. // This identifier is already stored in the model, so we simply return it color
  13. return QColor(m_model->data(m_model->index(row,1,QModelIndex()),Qt::DisplayRole).toString());
  14. }
  15. }
  16.  
  17. // This identifier is not already stored in the model, so we will add it at the end.
  18. m_model->insertRows(row,1,QModelIndex());
  19. m_model->setData(m_model->index(row,0,QModelIndex()),frame.id);
  20. m_model->setData(m_model->index(row,1,QModelIndex()),QColor(Qt::white).name());
  21. m_model->setData(m_model->index(row,2,QModelIndex()),"");
To copy to clipboard, switch view to plain text mode 

I am wondering if parsing the entire model is the correct way to do or if there is an other type of model I could use in order to make this more efficient.

Thanks.