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. Signal flag changes for QAbstractItemModel
Forum Update on Monday, May 27th 2025

Signal flag changes for QAbstractItemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
itemmodel
18 Posts 4 Posters 3.6k 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.
  • K Offline
    K Offline
    kshegunov
    Moderators
    wrote on 12 Apr 2019, 17:58 last edited by
    #1

    Hello,
    How do we signal item flags being changed for an item in the model?

    I have a model that's working over a network layer and I get unsolicited notifications for some of the items. What I need is to signal the view that a given item (or range of items) was disabled. However I don't see any signal or method that I'm supposed to call to achieve this.
    Should I raise dataChanged instead?

    Read and abide by the Qt Code of Conduct

    G 1 Reply Last reply 12 Apr 2019, 18:07
    0
    • K kshegunov
      12 Apr 2019, 17:58

      Hello,
      How do we signal item flags being changed for an item in the model?

      I have a model that's working over a network layer and I get unsolicited notifications for some of the items. What I need is to signal the view that a given item (or range of items) was disabled. However I don't see any signal or method that I'm supposed to call to achieve this.
      Should I raise dataChanged instead?

      G Offline
      G Offline
      Gojir4
      wrote on 12 Apr 2019, 18:07 last edited by
      #2

      @kshegunov Hi,

      If by "disabled" you mean that your item is still in the model, didn't have moved, so I would say yes, dataChanged is the right signal to call, with the corresponding indexes in arguments.

      K 1 Reply Last reply 12 Apr 2019, 18:49
      1
      • G Gojir4
        12 Apr 2019, 18:07

        @kshegunov Hi,

        If by "disabled" you mean that your item is still in the model, didn't have moved, so I would say yes, dataChanged is the right signal to call, with the corresponding indexes in arguments.

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 12 Apr 2019, 18:49 last edited by
        #3

        Yes, the item is still in the model and hasn't moved. What I want is to tell the view that it needs to call QAbstractItemModel::flags (which is going to return the flags with Qt::ItemIsEnabled bit removed) again for some range of items. If a role was changed then dataChanged would've been the correct way to do it that's clear enough, but the model interface doesn't have anything to signal changes for the item flags specifically as far as I can tell.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Apr 2019, 20:04 last edited by
          #4

          Hi,

          AFAIK, the flags are check "on use" so if you double click on an item which you just changed flags for not editable it should already take it into account.

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

          J 1 Reply Last reply 3 May 2023, 08:27
          2
          • S SGaist
            12 Apr 2019, 20:04

            Hi,

            AFAIK, the flags are check "on use" so if you double click on an item which you just changed flags for not editable it should already take it into account.

            J Offline
            J Offline
            JonB
            wrote on 3 May 2023, 08:27 last edited by
            #5

            @SGaist
            I come from looking at topic https://forum.qt.io/topic/144826/problem-using-an-integer-in-a-const-function-after-changing-its-value.

            I think your answer was "not good enough" :) , and @kshegunov's question is a good one. Let's say you do not "click an item", which doubtless would have caused that item to be re-evaluated/redrawn, but instead happen to do something in code which will cause flags() on some item to now return a different value. What is going to cause that item/flags to be re-evaluated? Nothing, I think.

            Since there is no "flags changed" signal I can only guess you would have to use dataChanged(), perhaps for DisplayRole or all roles, just to get e.g. a table view to update from the model's new flags.

            S 1 Reply Last reply 3 May 2023, 20:41
            0
            • J JonB
              3 May 2023, 08:27

              @SGaist
              I come from looking at topic https://forum.qt.io/topic/144826/problem-using-an-integer-in-a-const-function-after-changing-its-value.

              I think your answer was "not good enough" :) , and @kshegunov's question is a good one. Let's say you do not "click an item", which doubtless would have caused that item to be re-evaluated/redrawn, but instead happen to do something in code which will cause flags() on some item to now return a different value. What is going to cause that item/flags to be re-evaluated? Nothing, I think.

              Since there is no "flags changed" signal I can only guess you would have to use dataChanged(), perhaps for DisplayRole or all roles, just to get e.g. a table view to update from the model's new flags.

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 3 May 2023, 20:41 last edited by
              #6

              @JonB indeed, there's nothing to notify that specific change.
              I am wondering whether dataChanged conveys that idea at all since it's there was in fact nothing changed data wise.

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

              J 1 Reply Last reply 3 May 2023, 20:51
              0
              • S SGaist
                3 May 2023, 20:41

                @JonB indeed, there's nothing to notify that specific change.
                I am wondering whether dataChanged conveys that idea at all since it's there was in fact nothing changed data wise.

                J Offline
                J Offline
                JonB
                wrote on 3 May 2023, 20:51 last edited by JonB 5 Mar 2023, 20:53
                #7

                @SGaist
                dataChanged() does not really convey that. But as @kshegunov said there isn't a role for "flags". I imagine DisplayRole will have the desired effect of forcing it to re-evaluate the flag()s and see e.g. item enablement change, so you would get the desired behaviour for the wrong reason.

                S 1 Reply Last reply 3 May 2023, 20:53
                0
                • J JonB
                  3 May 2023, 20:51

                  @SGaist
                  dataChanged() does not really convey that. But as @kshegunov said there isn't a role for "flags". I imagine DisplayRole will have the desired effect of forcing it to re-evaluate the flag()s and see e.g. item enablement change, so you would get the desired behaviour for the wrong reason.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 3 May 2023, 20:53 last edited by
                  #8

                  @JonB maybe coupled with EditRole since the flag change might also influence that part.

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

                  J 1 Reply Last reply 3 May 2023, 20:55
                  0
                  • S SGaist
                    3 May 2023, 20:53

                    @JonB maybe coupled with EditRole since the flag change might also influence that part.

                    J Offline
                    J Offline
                    JonB
                    wrote on 3 May 2023, 20:55 last edited by
                    #9

                    @SGaist Do a dataChanged() for "all roles" and then you have done the best you can :)

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 5 May 2023, 18:55 last edited by
                      #10

                      I am sure there are some corner cases that will be fun

                      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
                      • K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 6 May 2023, 08:13 last edited by
                        #11

                        Well, I'd long forgotten about this topic, but I would think we actually need a flagsChanged(QModelIndex, QModelIndex) signal, which would be "the right way"™. I didn't open a feature request at the time, but maybe one of you might feel strongly enough about it to do so.

                        Read and abide by the Qt Code of Conduct

                        J 1 Reply Last reply 6 May 2023, 08:21
                        1
                        • K kshegunov
                          6 May 2023, 08:13

                          Well, I'd long forgotten about this topic, but I would think we actually need a flagsChanged(QModelIndex, QModelIndex) signal, which would be "the right way"™. I didn't open a feature request at the time, but maybe one of you might feel strongly enough about it to do so.

                          J Offline
                          J Offline
                          JonB
                          wrote on 6 May 2023, 08:21 last edited by
                          #12

                          @kshegunov
                          It would indeed! But then the issue is: who wants to go through all the code finding every occurrence where flags() has changed to decide what needs to be done in response? And in practice would it be any more or less than dataChanged(index, index, {}) does? :)

                          K 1 Reply Last reply 6 May 2023, 08:24
                          0
                          • J JonB
                            6 May 2023, 08:21

                            @kshegunov
                            It would indeed! But then the issue is: who wants to go through all the code finding every occurrence where flags() has changed to decide what needs to be done in response? And in practice would it be any more or less than dataChanged(index, index, {}) does? :)

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 6 May 2023, 08:24 last edited by
                            #13

                            @JonB said in Signal flag changes for QAbstractItemModel:

                            It would indeed! But then the issue is: who wants to go through all the code finding every occurrence where flags() has changed to decide what needs to be done in response?

                            It should be painless in the sense that it's a new signal, so nobody needs to actually do anything to keep old code working.

                            And in practice would it be any more or less than dataChanged(index, index, {}) does? :)

                            Possibly, as you don't go around querying the data from the model, albeit I'm not sure if that's really relevant, as in the end you may need to.

                            Read and abide by the Qt Code of Conduct

                            J 1 Reply Last reply 6 May 2023, 08:32
                            0
                            • K kshegunov
                              6 May 2023, 08:24

                              @JonB said in Signal flag changes for QAbstractItemModel:

                              It would indeed! But then the issue is: who wants to go through all the code finding every occurrence where flags() has changed to decide what needs to be done in response?

                              It should be painless in the sense that it's a new signal, so nobody needs to actually do anything to keep old code working.

                              And in practice would it be any more or less than dataChanged(index, index, {}) does? :)

                              Possibly, as you don't go around querying the data from the model, albeit I'm not sure if that's really relevant, as in the end you may need to.

                              J Offline
                              J Offline
                              JonB
                              wrote on 6 May 2023, 08:32 last edited by
                              #14

                              @kshegunov said in Signal flag changes for QAbstractItemModel:

                              Possibly, as you don't go around querying the data from the model, albeit I'm not sure if that's really relevant, as in the end you may need to.

                              Because of, say, Qt::ItemIsEnabled flag, which shows item differently, won't it end up having to access item's DisplayRole which has to query the model to get the text again to show it dimmed?

                              K 1 Reply Last reply 6 May 2023, 08:37
                              0
                              • J JonB
                                6 May 2023, 08:32

                                @kshegunov said in Signal flag changes for QAbstractItemModel:

                                Possibly, as you don't go around querying the data from the model, albeit I'm not sure if that's really relevant, as in the end you may need to.

                                Because of, say, Qt::ItemIsEnabled flag, which shows item differently, won't it end up having to access item's DisplayRole which has to query the model to get the text again to show it dimmed?

                                K Offline
                                K Offline
                                kshegunov
                                Moderators
                                wrote on 6 May 2023, 08:37 last edited by
                                #15

                                @JonB said in Signal flag changes for QAbstractItemModel:

                                Because of, say, Qt::ItemIsEnabled flag, which shows item differently, won't it end up having to access item's DisplayRole which has to query the model to get the text again to show it dimmed?

                                It may, which was my original argument - it (the signal) could simply do nothing significant in the end.

                                Read and abide by the Qt Code of Conduct

                                S 1 Reply Last reply 6 May 2023, 19:11
                                0
                                • K kshegunov
                                  6 May 2023, 08:37

                                  @JonB said in Signal flag changes for QAbstractItemModel:

                                  Because of, say, Qt::ItemIsEnabled flag, which shows item differently, won't it end up having to access item's DisplayRole which has to query the model to get the text again to show it dimmed?

                                  It may, which was my original argument - it (the signal) could simply do nothing significant in the end.

                                  S Offline
                                  S Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on 6 May 2023, 19:11 last edited by
                                  #16

                                  I wonder how often this use case happens.

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

                                  K 1 Reply Last reply 9 May 2023, 08:42
                                  1
                                  • S SGaist
                                    6 May 2023, 19:11

                                    I wonder how often this use case happens.

                                    K Offline
                                    K Offline
                                    kshegunov
                                    Moderators
                                    wrote on 9 May 2023, 08:42 last edited by
                                    #17

                                    @SGaist said in Signal flag changes for QAbstractItemModel:

                                    I wonder how often this use case happens.

                                    I'd imagine for any nontrivial use of the item model, this will happen. In the case of widgets it probably won't matter anyway, as you'd want to immediately redraw whatever it is you're showing; but with Qt quick it may actually be a malice on performance, since it would invalidate the scene graph node (which may or may not be a problem, I haven't actually looked at the implementation).

                                    Read and abide by the Qt Code of Conduct

                                    J 1 Reply Last reply 9 May 2023, 08:48
                                    0
                                    • K kshegunov
                                      9 May 2023, 08:42

                                      @SGaist said in Signal flag changes for QAbstractItemModel:

                                      I wonder how often this use case happens.

                                      I'd imagine for any nontrivial use of the item model, this will happen. In the case of widgets it probably won't matter anyway, as you'd want to immediately redraw whatever it is you're showing; but with Qt quick it may actually be a malice on performance, since it would invalidate the scene graph node (which may or may not be a problem, I haven't actually looked at the implementation).

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 9 May 2023, 08:48 last edited by
                                      #18

                                      @kshegunov
                                      One might actually need the signal to tell us which flags have changed. Only certain flag changes affect anything we are interested in, several seem not relevant.

                                      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