Hi everyone,

I'm new to Qt and would really appreciate help on a problem I have been having. I am doing a simple database program in C++ and I want to search through the products in one QSqlTableModel (modelInventory) for matches to a string and transfer the matching products to another QSqlTableModel (modelSearchResult).

Qt Code:
  1. for (int i_count=0; i_count < (modelInventory->rowCount()); i_count++)
  2. {
  3.  
  4. //The name of the current product being checked in the inventory model
  5. QString tempProdName = modelInventory->record(i_count).value(1).toString();
  6.  
  7. QSqlRecord setterRecord = modelSearchResult->record(); //A record to set the search
  8. //result model with
  9.  
  10. //If there is a substring match between the current product name and the search
  11. //string (both ways), transfer all the information of that inventory record
  12. //and put it into the temporary search result record "setRecord". Also,
  13. //store the current "i_count" value into the eighth field of the search
  14. //result record to remember the index of the record
  15. if ( ( tempProdName.indexOf(searchString,0,Qt::CaseInsensitive) != -1 )
  16. || ( searchString.indexOf(tempProdName,0,Qt::CaseInsensitive) != -1) )
  17. {
  18.  
  19. //Transfer over all the field information as well as the current index
  20. //being searched
  21. setterRecord.setValue(0, modelInventory->record(i_count).value(0));
  22. setterRecord.setValue(1, modelInventory->record(i_count).value(1));
  23. setterRecord.setValue(2, modelInventory->record(i_count).value(2));
  24. setterRecord.setValue(3, modelInventory->record(i_count).value(3));
  25. setterRecord.setValue(4, modelInventory->record(i_count).value(4));
  26. setterRecord.setValue(5, modelInventory->record(i_count).value(5));
  27. setterRecord.setValue(6, modelInventory->record(i_count).value(6));
  28. setterRecord.setValue(7, modelInventory->record(i_count).value(7));
  29. setterRecord.setValue(8, QVariant(i_count)); //Store the index of the record in the
  30. //inventory model in the search result
  31. //model
  32.  
  33. //Append the new search match into the search result model
  34. modelSearchResult->insertRecord(-1, setterRecord);
  35.  
  36. } //END if ( ( tempProdName.indexOf(searchString,0,Qt::CaseInsensitive) != -1 )
  37. // || ( searchString.indexOf(tempProdName,0,Qt::CaseInsensitive) != -1) )
  38.  
  39.  
  40. } //END for (int i_count=0; i_count< (modelInventory->rowCount()); i_count++)
To copy to clipboard, switch view to plain text mode 

When I comment out the for loop and replace it with: int i_count = 2; it will be able to parse the third (2+ 1) record in the inventory model. So is there something wrong with my for loop? I know that the rowCount() return the right number of rows.

I would really appreciate if you guys could help me, thanks!