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. Keeping filtered view up to date
QtWS25 Last Chance

Keeping filtered view up to date

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 637 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.
  • ozcanayO Offline
    ozcanayO Offline
    ozcanay
    wrote on last edited by ozcanay
    #1

    Hi,

    I have a widget that has a custom QAbstractTableModel that is proxied to a custom QSortFilterProxyModel. Widget displays orders coming from the exchange. There is a filter functionality on widget. For example, if you want to display orders only with state "New", you can do so via an input field provided on UI. The problem here is that when the state of the order changes (to "Filled" for example), the view is not kept up to date, i.e. orders with state "Filled" stays on the view. In order to filter "New" orders again, I have to apply filter again. What might be the problem here? Let me know if I should provide any code snippet for the solution.

    JonBJ 1 Reply Last reply
    0
    • ozcanayO ozcanay

      Hi,

      I have a widget that has a custom QAbstractTableModel that is proxied to a custom QSortFilterProxyModel. Widget displays orders coming from the exchange. There is a filter functionality on widget. For example, if you want to display orders only with state "New", you can do so via an input field provided on UI. The problem here is that when the state of the order changes (to "Filled" for example), the view is not kept up to date, i.e. orders with state "Filled" stays on the view. In order to filter "New" orders again, I have to apply filter again. What might be the problem here? Let me know if I should provide any code snippet for the solution.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ozcanay
      I believe you mean: you update a field in a row so that it should no longer be shown according to the current QSortFilterProxyModel filter setting? That should happen automatically when your base model emits the dataChanged() signal, so that the view will know to re-evaluate whether that row should/should not be visible. Show your code if necessary.

      1 Reply Last reply
      2
      • ozcanayO Offline
        ozcanayO Offline
        ozcanay
        wrote on last edited by ozcanay
        #3

        I am actually already doing what you said. In my BaseModel, when an an entry is updated I do:

        emit dataChanged(index(row_index, 0), index(row_index, column_count_));

        In the proxy model that proxies this base model I call a slot named onSourceDataChanged upon dataChanged signal emitted from the base model.

        void ProxyModel::onSourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles)
        {
            emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight), roles);
        }
        

        I connect this signal-slot relation in widget that displays the model:

        QObject::connect(base_model, &BaseModel::dataChanged, proxy_model, &ProxyModel::onSourceDataChanged);
        

        When I filter the view, I can see that the view is updated, some cells keep getting updated as well as the state. So, I believe I am emitting dataChanged signal as intended. However, filter is not respected.

        JonBJ Christian EhrlicherC 2 Replies Last reply
        0
        • ozcanayO ozcanay

          I am actually already doing what you said. In my BaseModel, when an an entry is updated I do:

          emit dataChanged(index(row_index, 0), index(row_index, column_count_));

          In the proxy model that proxies this base model I call a slot named onSourceDataChanged upon dataChanged signal emitted from the base model.

          void ProxyModel::onSourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles)
          {
              emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight), roles);
          }
          

          I connect this signal-slot relation in widget that displays the model:

          QObject::connect(base_model, &BaseModel::dataChanged, proxy_model, &ProxyModel::onSourceDataChanged);
          

          When I filter the view, I can see that the view is updated, some cells keep getting updated as well as the state. So, I believe I am emitting dataChanged signal as intended. However, filter is not respected.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @ozcanay
          I don't know. Try void QSortFilterProxyModel::invalidate() to verify that updates.

          BTW, I may be wrong, but I would not expect to need your mapped signal. So far as I know, even if the view is connected through a proxy it will still correctly see source model changes/dataChanged() signals and act on them. Indeed, you might just try removing your connect(&ProxyModel::onSourceDataChanged) to (a) verify and (b) just in case that is actaully interfering.

          So far as I recall, I used QSortFilterProxyModel with no extra code and views updated correctly. Unless anyone corrects me on this..... (I suppose it's possible I noticed sort changes correctly reflected but did not try filter ones.)

          1 Reply Last reply
          0
          • ozcanayO ozcanay

            I am actually already doing what you said. In my BaseModel, when an an entry is updated I do:

            emit dataChanged(index(row_index, 0), index(row_index, column_count_));

            In the proxy model that proxies this base model I call a slot named onSourceDataChanged upon dataChanged signal emitted from the base model.

            void ProxyModel::onSourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList<int> &roles)
            {
                emit dataChanged(mapFromSource(topLeft), mapFromSource(bottomRight), roles);
            }
            

            I connect this signal-slot relation in widget that displays the model:

            QObject::connect(base_model, &BaseModel::dataChanged, proxy_model, &ProxyModel::onSourceDataChanged);
            

            When I filter the view, I can see that the view is updated, some cells keep getting updated as well as the state. So, I believe I am emitting dataChanged signal as intended. However, filter is not respected.

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @ozcanay said in Keeping filtered view up to date:

            emit dataChanged(index(row_index, 0), index(row_index, column_count_));

            This is wrong - index(row_index, column_count_) is invalid.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            VRoninV ozcanayO 2 Replies Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

              @ozcanay said in Keeping filtered view up to date:

              emit dataChanged(index(row_index, 0), index(row_index, column_count_));

              This is wrong - index(row_index, column_count_) is invalid.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              @Christian-Ehrlicher said in Keeping filtered view up to date:

              This is wrong - index(row_index, column_count_) is invalid.

              Funnily enough the changes @Christian-Ehrlicher implemented to the code of the views make it so the views will still update correctly. Unfortunately QSortFilterProxyModel is not as smart and will just do nothing if one of the indexes is invalid. i suspect it should be index(row_index, column_count_-1)

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              ozcanayO 2 Replies Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @ozcanay said in Keeping filtered view up to date:

                emit dataChanged(index(row_index, 0), index(row_index, column_count_));

                This is wrong - index(row_index, column_count_) is invalid.

                ozcanayO Offline
                ozcanayO Offline
                ozcanay
                wrote on last edited by ozcanay
                #7

                @Christian-Ehrlicher

                What is invalid about it? Is it just the fact that column_count_ should be column_count_ - 1 or is there something else as well?

                Christian EhrlicherC 1 Reply Last reply
                0
                • ozcanayO ozcanay

                  @Christian-Ehrlicher

                  What is invalid about it? Is it just the fact that column_count_ should be column_count_ - 1 or is there something else as well?

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @ozcanay said in Keeping filtered view up to date:

                  Is it just the fact that column_count_ should be column_count_ - 1

                  correct

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • VRoninV VRonin

                    @Christian-Ehrlicher said in Keeping filtered view up to date:

                    This is wrong - index(row_index, column_count_) is invalid.

                    Funnily enough the changes @Christian-Ehrlicher implemented to the code of the views make it so the views will still update correctly. Unfortunately QSortFilterProxyModel is not as smart and will just do nothing if one of the indexes is invalid. i suspect it should be index(row_index, column_count_-1)

                    ozcanayO Offline
                    ozcanayO Offline
                    ozcanay
                    wrote on last edited by ozcanay
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • VRoninV VRonin

                      @Christian-Ehrlicher said in Keeping filtered view up to date:

                      This is wrong - index(row_index, column_count_) is invalid.

                      Funnily enough the changes @Christian-Ehrlicher implemented to the code of the views make it so the views will still update correctly. Unfortunately QSortFilterProxyModel is not as smart and will just do nothing if one of the indexes is invalid. i suspect it should be index(row_index, column_count_-1)

                      ozcanayO Offline
                      ozcanayO Offline
                      ozcanay
                      wrote on last edited by ozcanay
                      #10

                      @VRonin said in Keeping filtered view up to date:

                      @Christian-Ehrlicher said in Keeping filtered view up to date:

                      This is wrong - index(row_index, column_count_) is invalid.

                      Funnily enough the changes @Christian-Ehrlicher implemented to the code of the views make it so the views will still update correctly. Unfortunately QSortFilterProxyModel is not as smart and will just do nothing if one of the indexes is invalid. i suspect it should be index(row_index, column_count_-1)

                      Even though obviously what I have done is wrong, this still smells like a bug to me. I would expect uniform behavior from Qt.

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • ozcanayO ozcanay

                        @VRonin said in Keeping filtered view up to date:

                        @Christian-Ehrlicher said in Keeping filtered view up to date:

                        This is wrong - index(row_index, column_count_) is invalid.

                        Funnily enough the changes @Christian-Ehrlicher implemented to the code of the views make it so the views will still update correctly. Unfortunately QSortFilterProxyModel is not as smart and will just do nothing if one of the indexes is invalid. i suspect it should be index(row_index, column_count_-1)

                        Even though obviously what I have done is wrong, this still smells like a bug to me. I would expect uniform behavior from Qt.

                        Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @ozcanay If you pass mess in you will get mess out. Imo there will be even a warning message when your Qt libs build in debug mode.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        3
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #12

                          When subclassing models, passing them through the model test is always advisable. Mistakes like this one are easily spotted by that tool

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          4

                          • Login

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