Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlTableModel Filter
Forum Update on Monday, May 27th 2025

QSqlTableModel Filter

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqltablemodelfilter
4 Posts 3 Posters 644 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saviz
    wrote on 10 Feb 2023, 01:36 last edited by Saviz 2 Oct 2023, 01:38
    #1

    I have a SQLite data base that I am displaying with the help of "QSqlTableModel" and "QTabelView". Now I am trying to set a filter for my model.

    Here is the code:

    void DataBase::method_set_filter(const QString &filterArgument)
    {
        QString command(
                    "'%1' LIKE '%%2%'"
                    );
    
        command = command.arg(
    
                    // 1
                    DataBase::COLUMN_FIRSTNAME, // Stored as static const in my class
                    // 2
                    filterArgument
                    );
    
        qDebug() << command;
        qDebug() << tableModel->lastError();
    
    
        tableModel->setFilter(command);
        tableModel->select();
    }
    

    Here is how my table looks before setting filter:
    Table.png

    If I try to search for the first name of "Jacob", then it will show me nothing:
    Table after.png

    What my debug statements show:

    "'First name' LIKE '%Jacob%'"
    QSqlError("", "", "")
    

    Why is this happening?

    P J 2 Replies Last reply 10 Feb 2023, 01:56
    0
    • S Saviz
      10 Feb 2023, 01:36

      I have a SQLite data base that I am displaying with the help of "QSqlTableModel" and "QTabelView". Now I am trying to set a filter for my model.

      Here is the code:

      void DataBase::method_set_filter(const QString &filterArgument)
      {
          QString command(
                      "'%1' LIKE '%%2%'"
                      );
      
          command = command.arg(
      
                      // 1
                      DataBase::COLUMN_FIRSTNAME, // Stored as static const in my class
                      // 2
                      filterArgument
                      );
      
          qDebug() << command;
          qDebug() << tableModel->lastError();
      
      
          tableModel->setFilter(command);
          tableModel->select();
      }
      

      Here is how my table looks before setting filter:
      Table.png

      If I try to search for the first name of "Jacob", then it will show me nothing:
      Table after.png

      What my debug statements show:

      "'First name' LIKE '%Jacob%'"
      QSqlError("", "", "")
      

      Why is this happening?

      P Online
      P Online
      Pl45m4
      wrote on 10 Feb 2023, 01:56 last edited by Pl45m4 2 Oct 2023, 02:06
      #2

      @Saviz said in QSqlTableModel Filter:

      Why is this happening?

      Because

      "'%1' LIKE '%%2%'"

      What do you get with
      " '%1' LIKE '%2'"
      as command?

      It doesnt seem to resolve the wildcards.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • S Saviz
        10 Feb 2023, 01:36

        I have a SQLite data base that I am displaying with the help of "QSqlTableModel" and "QTabelView". Now I am trying to set a filter for my model.

        Here is the code:

        void DataBase::method_set_filter(const QString &filterArgument)
        {
            QString command(
                        "'%1' LIKE '%%2%'"
                        );
        
            command = command.arg(
        
                        // 1
                        DataBase::COLUMN_FIRSTNAME, // Stored as static const in my class
                        // 2
                        filterArgument
                        );
        
            qDebug() << command;
            qDebug() << tableModel->lastError();
        
        
            tableModel->setFilter(command);
            tableModel->select();
        }
        

        Here is how my table looks before setting filter:
        Table.png

        If I try to search for the first name of "Jacob", then it will show me nothing:
        Table after.png

        What my debug statements show:

        "'First name' LIKE '%Jacob%'"
        QSqlError("", "", "")
        

        Why is this happening?

        J Online
        J Online
        JonB
        wrote on 10 Feb 2023, 08:17 last edited by JonB 2 Oct 2023, 08:31
        #3

        @Saviz

        • bool QSqlTableModel::select() returns a bool so that you can test it.

        • tableModel->lastError() won't return anything immediately after you have set QString command, that is just a string variable assignment. Examine it after the select() returns false.

        • Don't name database columns with spaces in them, it's just asking for trouble, because...

        • ...Most importantly: 'First name' LIKE '%Jacob%' where do you ever get that SQLite uses '...' (single quotes) to enclose a column name with space in it? It doesn't. Consequently filtering with most strings, include Jacob, will always return no rows; if you try filtering by, say, rst it will return every row :)

        P 1 Reply Last reply 10 Feb 2023, 11:11
        3
        • J JonB
          10 Feb 2023, 08:17

          @Saviz

          • bool QSqlTableModel::select() returns a bool so that you can test it.

          • tableModel->lastError() won't return anything immediately after you have set QString command, that is just a string variable assignment. Examine it after the select() returns false.

          • Don't name database columns with spaces in them, it's just asking for trouble, because...

          • ...Most importantly: 'First name' LIKE '%Jacob%' where do you ever get that SQLite uses '...' (single quotes) to enclose a column name with space in it? It doesn't. Consequently filtering with most strings, include Jacob, will always return no rows; if you try filtering by, say, rst it will return every row :)

          P Online
          P Online
          Pl45m4
          wrote on 10 Feb 2023, 11:11 last edited by
          #4

          @JonB said in QSqlTableModel Filter:

          'First name' LIKE '%Jacob%'

          lol, was checking the condition and haven't noticed that, because of the use of DataBase::COLUMN_FIRSTNAME


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          0

          1/4

          10 Feb 2023, 01:36

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved