PDA

View Full Version : Problem with Parsing Items in a QSqlTable Model



weevle123
3rd May 2010, 04:43
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).




for (int i_count=0; i_count < (modelInventory->rowCount()); i_count++)
{

//The name of the current product being checked in the inventory model
QString tempProdName = modelInventory->record(i_count).value(1).toString();

QSqlRecord setterRecord = modelSearchResult->record(); //A record to set the search
//result model with

//If there is a substring match between the current product name and the search
//string (both ways), transfer all the information of that inventory record
//and put it into the temporary search result record "setRecord". Also,
//store the current "i_count" value into the eighth field of the search
//result record to remember the index of the record
if ( ( tempProdName.indexOf(searchString,0,Qt::CaseInsens itive) != -1 )
|| ( searchString.indexOf(tempProdName,0,Qt::CaseInsens itive) != -1) )
{

//Transfer over all the field information as well as the current index
//being searched
setterRecord.setValue(0, modelInventory->record(i_count).value(0));
setterRecord.setValue(1, modelInventory->record(i_count).value(1));
setterRecord.setValue(2, modelInventory->record(i_count).value(2));
setterRecord.setValue(3, modelInventory->record(i_count).value(3));
setterRecord.setValue(4, modelInventory->record(i_count).value(4));
setterRecord.setValue(5, modelInventory->record(i_count).value(5));
setterRecord.setValue(6, modelInventory->record(i_count).value(6));
setterRecord.setValue(7, modelInventory->record(i_count).value(7));
setterRecord.setValue(8, QVariant(i_count)); //Store the index of the record in the
//inventory model in the search result
//model

//Append the new search match into the search result model
modelSearchResult->insertRecord(-1, setterRecord);

} //END if ( ( tempProdName.indexOf(searchString,0,Qt::CaseInsens itive) != -1 )
// || ( searchString.indexOf(tempProdName,0,Qt::CaseInsens itive) != -1) )


} //END for (int i_count=0; i_count< (modelInventory->rowCount()); i_count++)




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!

Lykurg
3rd May 2010, 07:55
It's not direct an answer to your question but why do you don't use pure SQL for that? It should be much easier. Assuming you are using MySql it could be:INSERT INTO modelSearchResult (field1, field2, ...) SELECT field1, field2, ... FROM modelInventory WHERE id IN (1,2,3,...); So you once query the id's of the original table matching your search and then simply execute one SQL command and all is fine.

weevle123
4th May 2010, 03:49
I am using SQLite3, and modelSearchResult is a QSqlTableModel, not an actual SQL table itself.

I am populating modelInventory (also a QSqlTableModel) from the SQL table "inventory".

Anyway, what Lykurg said gave me the idea to also connected modelSearchResult to the same "inventory" table as well. It works now: (but there is a but)




modelSearchResult->setTable("inventory"); //Connect to same SQL table as modelInventory

QString filterString = "prodName LIKE '%" + searchString + "%'"; //prodName is the name of a field in the SQL table

modelSearchResult->setFilter(filterString); //Apply the filter

modelSearchResult->select(); //Populate the search result table




While this does find the correct records, how would I keep track of the row index of the product in the inventory model, since I need to keep it somewhere in my search result model to allow the user to edit both data from both models at the same time when the search result view is clicked (I am using onManualSubmit, so I don't want changes in the database to automatically occur).

Thanks Lykurg, and if you have any other advise on this it would be greatly appreciated.

weevle123
6th May 2010, 04:02
Never mind, I think I got it. I just added a INTEGER PRIMARY KEY AUTO INCREMENT to the SQL database and check for that.