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
Qt 6.11 is out! See what's new in the release blog

QSqlTableModel Filter

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqltablemodelfilter
4 Posts 3 Posters 1.4k Views 1 Watching
  • 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.
  • SavizS Offline
    SavizS Offline
    Saviz
    wrote on last edited by Saviz
    #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?

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • SavizS Saviz

      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?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #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
      • SavizS Saviz

        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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #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 :)

        Pl45m4P 1 Reply Last reply
        3
        • JonBJ JonB

          @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 :)

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on 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

          • Login

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