Results 1 to 3 of 3

Thread: QSqlTableModel and LIKE

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlTableModel and LIKE

    Hi it's me again

    How can I filter a QSqlTableModel so that only rows show up that have a certain cell contain a substring, like the sql LIKE clause?
    The setFilter() member filters only on the whole string.

    I tried a query like:

    Qt Code:
    1. QString str = QString("SELECT * FROM pix WHERE 'name' LIKE '%%1%'").arg(nameEdit->text());
    2. QSqlQuery query(str);
    3. model->select();
    To copy to clipboard, switch view to plain text mode 

    But this seems not the way to go.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel and LIKE

    Use setFilter( QString("name LIKE '%%1%'").arg( str ) ), but make sure that "str" doesn't contain any malicious content.

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

    hrompato (12th January 2012)

  4. #3
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel and LIKE - SOLVED

    Thanks jacek, works right out of the box

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.