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 : SetQuery issue

QSqlTableModel : SetQuery issue

Scheduled Pinned Locked Moved General and Desktop
14 Posts 2 Posters 9.2k 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.
  • A Offline
    A Offline
    Antweb
    wrote on last edited by Antweb
    #1

    Hello there, I have initialized my QSqlTableModel like this:

    QSqlTableModel *model = new QSqlTableModel(this, db);
    model->setTable("path_master_table");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();

    ui->tableView->setModel(model);
    ui->tableView->sortByColumn(0, Qt::AscendingOrder);

    I can view my table very well.
    Now i want the table to change when i run a query on it. So I did this.

    QSqlQuery query(query_text); (where query text is a sql command)
    model->setQuery(query);

    I get the following error:
    error: 'void QSqlTableModel::setQuery(const QSqlQuery&)' is protected
    void setQuery(const QSqlQuery &query);
    ^

    error: within this context
    model->setQuery(query);
    ^

    Any Help?
    Thanks!

    p3c0P 1 Reply Last reply
    0
    • A Antweb

      Hello there, I have initialized my QSqlTableModel like this:

      QSqlTableModel *model = new QSqlTableModel(this, db);
      model->setTable("path_master_table");
      model->setEditStrategy(QSqlTableModel::OnManualSubmit);
      model->select();

      ui->tableView->setModel(model);
      ui->tableView->sortByColumn(0, Qt::AscendingOrder);

      I can view my table very well.
      Now i want the table to change when i run a query on it. So I did this.

      QSqlQuery query(query_text); (where query text is a sql command)
      model->setQuery(query);

      I get the following error:
      error: 'void QSqlTableModel::setQuery(const QSqlQuery&)' is protected
      void setQuery(const QSqlQuery &query);
      ^

      error: within this context
      model->setQuery(query);
      ^

      Any Help?
      Thanks!

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

      Hi @Antweb Since setQuery is protected you cannot use it directly but only in its subclass.

      You should normally not call it on a QSqlTableModel. Instead, use setTable(), setSort(), setFilter(), etc., to set up the query.

      Either use QSqlQueryModel or if you just want to filter the rows the use setFilter and specify the where clause there.

      157

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Antweb
        wrote on last edited by
        #3

        I did this but my program keeps crashing.

        QString value_entered = ui->lineEdit->text();
        QString query_text = "Route = '" + value_entered + "%'";
        model->setFilter(query_text);
        model->select();

        Can you tell why?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Antweb
          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 Antweb

            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
            • A Offline
              A Offline
              Antweb
              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 Antweb

                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
                • A Offline
                  A Offline
                  Antweb
                  wrote on last edited by
                  #8

                  Done. Thank you.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Antweb
                    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 Antweb

                      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
                      • A Offline
                        A Offline
                        Antweb
                        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 Antweb

                          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
                          • A Offline
                            A Offline
                            Antweb
                            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 Antweb

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