Results 1 to 5 of 5

Thread: QSqlTablemodel->setFilter()

  1. #1
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default QSqlTablemodel->setFilter()

    Hello friends,

    in my app I have three comboboxes, which I use as filter for my model.

    Qt Code:
    1. QString str1 = combo1->currentText();
    2. QString str2 = combo2->currentText();
    3. QString str3 = combo3->currentText();
    4.  
    5. QString filter1 = "field1='"+str1+"'";
    6. QString filter2 = "field2='"+str2+"'";
    7. QString filter3 = "field3= convert (datetime,'"+str3+"',120)";
    8.  
    9.  
    10. if (str1!="" && str2 != "" && str3 != "")//111
    11. model->setFilter( filter1 +" and "+ filter2 +" and "+filter3);
    12. else if (str1=="" && str2 != "" && str3 != "")//011
    13. model->setFilter( filter2 +" and "+filter3);
    14. else if (str1=="" && str2 != "" && str3 == "")//010
    15. model->setFilter( filter2 );
    16. else if (str1=="" && str2 == "" && str3 != "")//001
    17. model->setFilter( filter3 );
    18. else if (str1!="" && str2 != "" && str3 == "")//110
    19. model->setFilter( filter1 +" and "+ filter2 );
    20. else if (str1!="" && str2 == "" && str3 != "")//101
    21. model->setFilter( filter1 +" and "+ filter3 );
    22. else if (str1!="" && str2 == "" && str3 == "")//100
    23. model->setFilter( filter1 );
    24. else
    25. {
    26. model->setTable(tableName);
    27. model->select();
    28. }
    To copy to clipboard, switch view to plain text mode 

    As you can see I also give my combo a <""> for no filter, but I have always to check because a when I set a filter and when a filter has a "" it does not appear in my table.

    So when I have only 3 filter I have to check 2^3 choises that is 8 or four filter with 2^4=16 that is much coding. Is there a elegant way to setting the filters???

  2. #2
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlTablemodel->setFilter()

    Qt Code:
    1. QString text;
    2. if( str1.length() != 0 )
    3. text.append( filter1 );
    4.  
    5. if( str2.length() != 0 )
    6. {
    7. if( text.length() != 0 )
    8. text.append( " and " );
    9. text.append( filter2 );
    10. }
    11.  
    12. if( str3.length() != 0 )
    13. {
    14. if( text.length() != 0 )
    15. text.append( " and " );
    16. text.append( filter3 );
    17. }
    18.  
    19. model->setFilter( text );
    To copy to clipboard, switch view to plain text mode 

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

    codeman (9th June 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default Re: QSqlTablemodel->setFilter()

    I am surprised...... ok thank you very much cool solution

  5. #4
    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: QSqlTablemodel->setFilter()

    or use a QStringList like
    Qt Code:
    1. if (!myStringXYZ.isEmpty())
    2. myStringList.append(myStringXYZ);
    To copy to clipboard, switch view to plain text mode 
    and then get the full filter via
    Qt Code:
    1. myStringList.join(" and ");
    To copy to clipboard, switch view to plain text mode 
    this will produce a more readable code if you have more filters to check.

  6. #5
    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: QSqlTablemodel->setFilter()

    Quote Originally Posted by codeman View Post
    Qt Code:
    1. QString filter1 = "field1='"+str1+"'";
    To copy to clipboard, switch view to plain text mode 
    Is that for a sql query? Then better use QSqlQuery::prepare() with bindValue() to avoid sql injection.

Similar Threads

  1. QSqlTableModel & treeview
    By janEUcitzen in forum Qt Programming
    Replies: 6
    Last Post: 14th November 2008, 20:00
  2. QSqlTableModel inserts empty rows
    By Nesbitt in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2008, 12:47
  3. Replies: 4
    Last Post: 9th May 2008, 17:02
  4. Subclassing QSqlTableModel to overwrite setQuery
    By montuno in forum Qt Programming
    Replies: 3
    Last Post: 16th November 2007, 10:32
  5. QSqlTableModel
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 12:35

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.