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. Empty selection model after introducing QSqlTableModel->filter()
Forum Updated to NodeBB v4.3 + New Features

Empty selection model after introducing QSqlTableModel->filter()

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.3k 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.
  • artwawA Offline
    artwawA Offline
    artwaw
    wrote on last edited by artwaw
    #1

    Good afternoon,
    I can't retrieve selected items after filtering the model. Perhaps someone can shed some light on the possible solution. Part of my app is built as follows:

    SqLite db;
    QSqlTableModel reads from db.
    QTableView presents the data from model to the user.

    User can select rows (on or many) and then print them (single QPushButton).
    So far, it worked for over 2y. Recently I've been asked to introduce filtering, to be able to display only those rows that certain column, let's name it "type" has specified value or all the columns. What I did:
    QComboBox gathering all the possible live values from column "type" from db plus single option "no filter".
    As manual for QSqlTableModel states, filter property is for that, so I included slot (docs is of type QSqlTableModel):

    void MainWindow::applyItemFilter(QString filter) {
    if (filter == "No filter") {
    docs->setFilter("type is not null");
    } else {
    docs->setFilter("type = '"+filter+"'");
    };
    }

    Display works perfectly well but whenever I try to retrieve selected items via:

    for (int x = 0; x<ui->items->selectionModel()->selectedRows().count();x++) { code in here; }

    the count(); is equal to zero regardless of selection.

    What have I missed?

    Kind Regards,
    Artur Wawrowski

    For more information please re-read.

    Kind Regards,
    Artur

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      IIRC, setFilter will reload the data since it does a query to the database.

      Two solutions comes to mind:

      1. Use QSortFilterProxyModel.
      2. Store your selected data and once the filter is applied, re-select the data that are still available.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      artwawA 3 Replies Last reply
      2
      • SGaistS SGaist

        Hi,

        IIRC, setFilter will reload the data since it does a query to the database.

        Two solutions comes to mind:

        1. Use QSortFilterProxyModel.
        2. Store your selected data and once the filter is applied, re-select the data that are still available.
        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #3

        @SGaist Yes, invoking setFilter() implies select() and filter is basically (according to docs and verified by me just throwing out last query from the model after the filter is aplied) adding "where" clause the the query done by the model. That works as describes in documentation and I have no complaints whatsoever.

        But somehow the fact that I apply filter makes selectionModel in the view empty. I can try with QSortFilterProxyModel indeed, in fact I use it with a great success to provide fast search in different part of the program... I was just wondering if I missed something as documentation does not imply any sort of limitations with regards to QTableView and model utilizing a filter property. It is bizarre. If that is the only suggestion I will try it out tomorrow, luckily there is not much of change in the code to do that.

        I'll post the results in here. Thank you.

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          IIRC, setFilter will reload the data since it does a query to the database.

          Two solutions comes to mind:

          1. Use QSortFilterProxyModel.
          2. Store your selected data and once the filter is applied, re-select the data that are still available.
          artwawA Offline
          artwawA Offline
          artwaw
          wrote on last edited by
          #4

          @SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.

          For more information please re-read.

          Kind Regards,
          Artur

          SGaistS 1 Reply Last reply
          0
          • artwawA artwaw

            @SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @artwaw said in Empty selection model after introducing QSqlTableModel->filter():

            @SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.

            I'm not sure I'm following you on that one. How would you be saving the current selection ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            artwawA 1 Reply Last reply
            0
            • SGaistS SGaist

              @artwaw said in Empty selection model after introducing QSqlTableModel->filter():

              @SGaist Addendum, as per second point of your advice: after the select() forced by applying the filter model is left untouched (as select() removes selection, obviously, unless we catch up the signal and do as you advise) and if we select anything then - the behaviour is still invalid. So that solution does not apply in this case.

              I'm not sure I'm following you on that one. How would you be saving the current selection ?

              artwawA Offline
              artwawA Offline
              artwaw
              wrote on last edited by
              #6

              @SGaist That is the point, I don't have to. From the user perspective changing the filter makes selection invalid anyway so I do not have to worry about select() invalidating anything in the view.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                IIRC, setFilter will reload the data since it does a query to the database.

                Two solutions comes to mind:

                1. Use QSortFilterProxyModel.
                2. Store your selected data and once the filter is applied, re-select the data that are still available.
                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #7

                @SGaist Hi and apologies for late reply. I managed to make it work using QSortFilterProxyModel, as expected. Thank you.
                However odd behaviour of setFilter() should have reflection in docs for the future.

                Thank you for your help!

                For more information please re-read.

                Kind Regards,
                Artur

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  No worries !

                  Since you feel there's something missing, you can submit an improvement to the documentation and doing so make it available to other developers :)

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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