Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlTableModel : SetQuery issue
Qt 6.12 LTS is out! See what's new in the release blog

QSqlTableModel : SetQuery issue

Scheduled Pinned Locked Moved General and Desktop
14 Posts 2 Posters 12.0k Views 2 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #4

    I basically want to filter as I am entering route no in my lineedit. I have connected the lineedit valuechanged signal to a slot where I have written the above code.
    If not this way then what is the way to do that?

    p3c0P 1 Reply Last reply
    0
    • ? A Former User

      I basically want to filter as I am entering route no in my lineedit. I have connected the lineedit valuechanged signal to a slot where I have written the above code.
      If not this way then what is the way to do that?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #5

      @Antweb Shouldn't you use like when using % ? I'm not sure why it is crashing but since you want some realtime filtering its better to use QSortFilterProxyModel here. Checkout the examples Qt installed directory on your system.

      157

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #6

        I used like and the command is working if I call it in the constructor. But when I am calling in the slot connected to my valuechanged of lineedit it crashes. And I want to reduce the size of my table with each added character. I don't think that is possible with QSortFIlterProxyModel.

        p3c0P 1 Reply Last reply
        0
        • ? A Former User

          I used like and the command is working if I call it in the constructor. But when I am calling in the slot connected to my valuechanged of lineedit it crashes. And I want to reduce the size of my table with each added character. I don't think that is possible with QSortFIlterProxyModel.

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #7

          @Antweb OfCourse it is possible. You just have to re-implement filterAcceptsRow and put a condition as to what should be displayed. You have to just connect the textchanged signal to a slot where you will update the text using filterRegExp. Check the filtering subtopic for more details.

          157

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #8

            Done. Thank you.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by p3c0
              #9

              I am able to filter my QSqlTableModel with the help of text in lineedit. If simultaneously i want to filter the same table from selection in a combobox. How can i do that. Will I need a separate QSortFIlterProxyModel? Also i want the text selected from combobox to filter this QSqlTableModel on the basis of another Column.

              This is my code right now:

              QSqlTableModel *model = new QSqlTableModel(this, db);
              model->setTable("path_master_table");
              model->setEditStrategy(QSqlTableModel::OnManualSubmit);
              model->select();
              
              QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
              proxyModel->setSourceModel(model);
              
              ui->tableView->setModel(proxyModel);
              connect(ui->lineEdit, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterRegExp(QString)));
              

              Thanks.

              p3c0P 1 Reply Last reply
              0
              • ? A Former User

                I am able to filter my QSqlTableModel with the help of text in lineedit. If simultaneously i want to filter the same table from selection in a combobox. How can i do that. Will I need a separate QSortFIlterProxyModel? Also i want the text selected from combobox to filter this QSqlTableModel on the basis of another Column.

                This is my code right now:

                QSqlTableModel *model = new QSqlTableModel(this, db);
                model->setTable("path_master_table");
                model->setEditStrategy(QSqlTableModel::OnManualSubmit);
                model->select();
                
                QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
                proxyModel->setSourceModel(model);
                
                ui->tableView->setModel(proxyModel);
                connect(ui->lineEdit, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterRegExp(QString)));
                

                Thanks.

                p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #10

                @Antweb

                If simultaneously i want to filter the same table from selection in a combobox. How can i do that. Will I need a separate QSortFIlterProxyModel?

                No. Separate QSortFIlterProxyModel is not needed. Connect the signal currentIndexChanged which sends a QString to the slot where you can update filterRegExp with this text as done earlier.

                Also i want the text selected from combobox to filter this QSqlTableModel on the basis of another Column.

                In that case you will need to override filterAcceptsColumn.

                157

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #11

                  On what basis should i do the segregation while overriding.
                  Sorry, I am unable to figure out!

                  p3c0P 1 Reply Last reply
                  0
                  • ? A Former User

                    On what basis should i do the segregation while overriding.
                    Sorry, I am unable to figure out!

                    p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #12

                    @Antweb That will need subclassing. Filtering by column can also be done by filterKeyColumn.
                    Here is a complete example. It is for QTreeView but can also be done for QTableView is the same way.

                    157

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #13

                      I was able to do it with some simple signals and slots concept by resetting setFilterKeyColumn.
                      Complicating things further, is there a way to filter rows using the lineedit which have already been filtered once by the combobox?
                      Because right now it goes back to the original table and then filters!

                      p3c0P 1 Reply Last reply
                      0
                      • ? A Former User

                        I was able to do it with some simple signals and slots concept by resetting setFilterKeyColumn.
                        Complicating things further, is there a way to filter rows using the lineedit which have already been filtered once by the combobox?
                        Because right now it goes back to the original table and then filters!

                        p3c0P Offline
                        p3c0P Offline
                        p3c0
                        Moderators
                        wrote on last edited by p3c0
                        #14

                        @Antweb Unfortunately I too do not know how to filter a already filtered list. May be someone else can answer this question. You can start a new topic explain this requirement.

                        157

                        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
                        • Unsolved