Results 1 to 4 of 4

Thread: Problem with Parsing Items in a QSqlTable Model

  1. #1
    Join Date
    May 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with Parsing Items in a QSqlTable Model

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem with Parsing Items in a QSqlTable Model

    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:
    sql Code:
    1. INSERT INTO modelSearchResult (field1, field2, ...) SELECT field1, field2, ... FROM modelInventory WHERE id IN (1,2,3,...);
    To copy to clipboard, switch view to plain text mode 
    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.

  3. The following user says thank you to Lykurg for this useful post:

    weevle123 (4th May 2010)

  4. #3
    Join Date
    May 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with Parsing Items in a QSqlTable Model

    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)

    Qt Code:
    1. modelSearchResult->setTable("inventory"); //Connect to same SQL table as modelInventory
    2.  
    3. QString filterString = "prodName LIKE '%" + searchString + "%'"; //prodName is the name of a field in the SQL table
    4.  
    5. modelSearchResult->setFilter(filterString); //Apply the filter
    6.  
    7. modelSearchResult->select(); //Populate the search result table
    To copy to clipboard, switch view to plain text mode 


    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.

  5. #4
    Join Date
    May 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with Parsing Items in a QSqlTable Model

    Never mind, I think I got it. I just added a INTEGER PRIMARY KEY AUTO INCREMENT to the SQL database and check for that.

Similar Threads

  1. transfering items from one model to the other
    By ru_core in forum Qt Programming
    Replies: 4
    Last Post: 27th June 2008, 09:44
  2. Edit items in table model
    By gyre in forum Newbie
    Replies: 2
    Last Post: 14th January 2008, 04:08
  3. removing model Items
    By gyre in forum Newbie
    Replies: 2
    Last Post: 25th November 2007, 21:10
  4. Moving items within a model
    By iswm in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2007, 09:29
  5. Limitation when adding more than 256 items to a model
    By darkadept in forum Qt Programming
    Replies: 5
    Last Post: 25th May 2006, 16:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.