Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. QAbstractItemModel::setItemData partial success
Forum Updated to NodeBB v4.3 + New Features

QAbstractItemModel::setItemData partial success

Scheduled Pinned Locked Moved Brainstorm
18 Posts 7 Posters 5.5k Views 4 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.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #1

    Hi,
    The docs for QAbstractItemModel::setItemData state:

    bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
    Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole.
    Returns true if successful; otherwise returns false.
    Roles that are not in roles will not be modified.

    Now my question is, what would you expect it to return on a partial-success? i.e. if some but not all of the roles in the roles map are successfully set, would you expect it to return true or false?

    P.S.
    I know what the current behaviour is, I just want to know what a normal user would expect from that method given the documentation

    "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

    JonBJ kshegunovK 2 Replies Last reply
    0
    • J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      Hi,

      I would expect it to return false.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • VRoninV VRonin

        Hi,
        The docs for QAbstractItemModel::setItemData state:

        bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
        Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole.
        Returns true if successful; otherwise returns false.
        Roles that are not in roles will not be modified.

        Now my question is, what would you expect it to return on a partial-success? i.e. if some but not all of the roles in the roles map are successfully set, would you expect it to return true or false?

        P.S.
        I know what the current behaviour is, I just want to know what a normal user would expect from that method given the documentation

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

        @VRonin said in QAbstractItemModel::setItemData partial success:
        According to https://stackoverflow.com/questions/13265992/qabstractitemmodel-setitemdata-returns-false-when-inserting-qtuserrole

        QAbstractItemModel::setItemData is simply a convenience function which calls setData for each item in the roles map.

        bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
        {
            bool b = true;
            for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
                b = b && setData(index, it.value(), it.key());
            return b;
        }
        

        So it's b = b && .... If any fail, it would return false.

        VRoninV 1 Reply Last reply
        0
        • JonBJ JonB

          @VRonin said in QAbstractItemModel::setItemData partial success:
          According to https://stackoverflow.com/questions/13265992/qabstractitemmodel-setitemdata-returns-false-when-inserting-qtuserrole

          QAbstractItemModel::setItemData is simply a convenience function which calls setData for each item in the roles map.

          bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
          {
              bool b = true;
              for (QMap<int, QVariant>::ConstIterator it = roles.begin(); it != roles.end(); ++it)
                  b = b && setData(index, it.value(), it.key());
              return b;
          }
          

          So it's b = b && .... If any fail, it would return false.

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

          @JonB

          @VRonin said in QAbstractItemModel::setItemData partial success:

          P.S.
          I know what the current behaviour is, I just want to know what a normal user would expect from that method given the documentation

          "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

          JonBJ 1 Reply Last reply
          0
          • VRoninV VRonin

            @JonB

            @VRonin said in QAbstractItemModel::setItemData partial success:

            P.S.
            I know what the current behaviour is, I just want to know what a normal user would expect from that method given the documentation

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

            @VRonin
            Then since the docs don't state it's anyone's guess. Unless you have a pool of "normal users" whom you can poll... ;)

            VRoninV 1 Reply Last reply
            0
            • JonBJ JonB

              @VRonin
              Then since the docs don't state it's anyone's guess. Unless you have a pool of "normal users" whom you can poll... ;)

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

              @JonB said in QAbstractItemModel::setItemData partial success:

              Unless you have a pool of "normal users"

              Welcome to the pool my friend. What would you expect?

              "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

              JonBJ 1 Reply Last reply
              3
              • VRoninV VRonin

                @JonB said in QAbstractItemModel::setItemData partial success:

                Unless you have a pool of "normal users"

                Welcome to the pool my friend. What would you expect?

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

                @VRonin
                Oohhh, thank you for inviting me to participate.

                I happen to be working on an SMTP emailing thing atm, which takes multiple recipients to send to. It errors if none could be sent to, otherwise it returns a list of those recipients to whom it could not send.

                So I would expect QAbstractItemModel::setItemData to return a list of those roles which it could or could not send to!

                Given the bool return result however, if I had to bet I'd concur with @J-Hilk and go for false. But I'd still claim I might as well toss a coin.... :)

                1 Reply Last reply
                0
                • VRoninV VRonin

                  Hi,
                  The docs for QAbstractItemModel::setItemData state:

                  bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles)
                  Sets the role data for the item at index to the associated value in roles, for every Qt::ItemDataRole.
                  Returns true if successful; otherwise returns false.
                  Roles that are not in roles will not be modified.

                  Now my question is, what would you expect it to return on a partial-success? i.e. if some but not all of the roles in the roles map are successfully set, would you expect it to return true or false?

                  P.S.
                  I know what the current behaviour is, I just want to know what a normal user would expect from that method given the documentation

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  false, as any reasonable guess. If you have a compound operation, I'd expect it to fail as a whole whenever any or all of the constituent operations fail.

                  Read and abide by the Qt Code of Conduct

                  JKSHJ 1 Reply Last reply
                  0
                  • kshegunovK kshegunov

                    false, as any reasonable guess. If you have a compound operation, I'd expect it to fail as a whole whenever any or all of the constituent operations fail.

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @kshegunov said in QAbstractItemModel::setItemData partial success:

                    false, as any reasonable guess. If you have a compound operation, I'd expect it to fail as a whole whenever any or all of the constituent operations fail.

                    Agreed.

                    Transactions come to mind: If part of the operation fails, I'd expect the whole operation to be rolled back for integrity.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    JonBJ VRoninV 2 Replies Last reply
                    1
                    • JKSHJ JKSH

                      @kshegunov said in QAbstractItemModel::setItemData partial success:

                      false, as any reasonable guess. If you have a compound operation, I'd expect it to fail as a whole whenever any or all of the constituent operations fail.

                      Agreed.

                      Transactions come to mind: If part of the operation fails, I'd expect the whole operation to be rolled back for integrity.

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

                      @JKSH
                      For the record: setItemData(multiple-roles) does not behave like a transaction: it sets those roles it can, omits those roles it cannot, and returns false. No "rollback".

                      EDIT Looking at the code I posted earlier, the correct description of behaviour is:
                      It continues setting roles while successful. It stops setting roles as soon as one fails. And finally returns false.

                      1 Reply Last reply
                      1
                      • JKSHJ JKSH

                        @kshegunov said in QAbstractItemModel::setItemData partial success:

                        false, as any reasonable guess. If you have a compound operation, I'd expect it to fail as a whole whenever any or all of the constituent operations fail.

                        Agreed.

                        Transactions come to mind: If part of the operation fails, I'd expect the whole operation to be rolled back for integrity.

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

                        @JKSH said in QAbstractItemModel::setItemData partial success:

                        I'd expect the whole operation to be rolled back for integrity

                        Yes, that's what sparked my doubt as this is not what's happening in the Qt code

                        "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
                        0
                        • D Offline
                          D Offline
                          DavidFaure
                          wrote on last edited by VRonin
                          #12

                          Returning false is obviously what one would expect (this didn't succeed). But the real question is, should the supported roles be stored, or should the whole thing fail (not making any changes).

                          If I'm calling setItemData with text, pixmap and tooltip, and the underlying model is a QStringListModel, what should happen?
                          In the review I argued for "complete failure, nothing happens".

                          Looking at the specific example above, I can see how one could say "well, if you were calling setData three times instead, then the text would be set, and the pixmap+tooltip would be rejected, and in the end your text would be set". So one could say that for consistency setItemData should set the text.

                          What's for sure is that the QAIM implementation is wrong, it shouldn't stop at first error (that's based on the order of integral values for the roles... pretty arbitrary). Instead it should set as many as it can, and return false if any failed. Then that would match the above example...

                          Just some thoughts. I never actually used setItemData myself :-)

                          JKSHJ 1 Reply Last reply
                          3
                          • D DavidFaure

                            Returning false is obviously what one would expect (this didn't succeed). But the real question is, should the supported roles be stored, or should the whole thing fail (not making any changes).

                            If I'm calling setItemData with text, pixmap and tooltip, and the underlying model is a QStringListModel, what should happen?
                            In the review I argued for "complete failure, nothing happens".

                            Looking at the specific example above, I can see how one could say "well, if you were calling setData three times instead, then the text would be set, and the pixmap+tooltip would be rejected, and in the end your text would be set". So one could say that for consistency setItemData should set the text.

                            What's for sure is that the QAIM implementation is wrong, it shouldn't stop at first error (that's based on the order of integral values for the roles... pretty arbitrary). Instead it should set as many as it can, and return false if any failed. Then that would match the above example...

                            Just some thoughts. I never actually used setItemData myself :-)

                            JKSHJ Offline
                            JKSHJ Offline
                            JKSH
                            Moderators
                            wrote on last edited by
                            #13

                            @DavidFaure said in QAbstractItemModel::setItemData partial success:

                            Returning false is obviously what one would expect (this didn't succeed). But the real question is, should the supported roles be stored, or should the whole thing fail (not making any changes).

                            Currently, QAIM saves everything up to the first failure.

                            If we follow the principle of Transactions, nothing should be saved if any of them are invalid.

                            If I'm calling setItemData with text, pixmap and tooltip, and the underlying model is a QStringListModel, what should happen?
                            In the review I argued for "complete failure, nothing happens".

                            I agree with this argument :)

                            Looking at the specific example above, I can see how one could say "well, if you were calling setData three times instead, then the text would be set, and the pixmap+tooltip would be rejected, and in the end your text would be set". So one could say that for consistency setItemData should set the text.

                            I consider the 3x setData() as 3 separate transactions (like executing 3 SQL queries in a row) and setItemData() as 1 transaction.

                            What's for sure is that the QAIM implementation is wrong, it shouldn't stop at first error (that's based on the order of integral values for the roles... pretty arbitrary). Instead it should set as many as it can, and return false if any failed. Then that would match the above example...

                            Agreed, stopping at the first error is the worst of both worlds.

                            My preference is for the strict transaction approach. If that's rejected, I would then go for storing every valid role.

                            (Having said that, we shouldn't change the existing behaviour before Qt 6 as it risks breaking existing code)

                            Just some thoughts. I never actually used setItemData myself :-)

                            That's the perfect scenario for bike-shedding! :-D

                            Disclaimer: I haven't used it either

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            VRoninV 1 Reply Last reply
                            1
                            • JKSHJ JKSH

                              @DavidFaure said in QAbstractItemModel::setItemData partial success:

                              Returning false is obviously what one would expect (this didn't succeed). But the real question is, should the supported roles be stored, or should the whole thing fail (not making any changes).

                              Currently, QAIM saves everything up to the first failure.

                              If we follow the principle of Transactions, nothing should be saved if any of them are invalid.

                              If I'm calling setItemData with text, pixmap and tooltip, and the underlying model is a QStringListModel, what should happen?
                              In the review I argued for "complete failure, nothing happens".

                              I agree with this argument :)

                              Looking at the specific example above, I can see how one could say "well, if you were calling setData three times instead, then the text would be set, and the pixmap+tooltip would be rejected, and in the end your text would be set". So one could say that for consistency setItemData should set the text.

                              I consider the 3x setData() as 3 separate transactions (like executing 3 SQL queries in a row) and setItemData() as 1 transaction.

                              What's for sure is that the QAIM implementation is wrong, it shouldn't stop at first error (that's based on the order of integral values for the roles... pretty arbitrary). Instead it should set as many as it can, and return false if any failed. Then that would match the above example...

                              Agreed, stopping at the first error is the worst of both worlds.

                              My preference is for the strict transaction approach. If that's rejected, I would then go for storing every valid role.

                              (Having said that, we shouldn't change the existing behaviour before Qt 6 as it risks breaking existing code)

                              Just some thoughts. I never actually used setItemData myself :-)

                              That's the perfect scenario for bike-shedding! :-D

                              Disclaimer: I haven't used it either

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

                              @JKSH said in QAbstractItemModel::setItemData partial success:

                              My preference is for the strict transaction approach.

                              That's easy to implement in specific models like QStringListModel but close to impossible in the generic implementation

                              @JKSH said in QAbstractItemModel::setItemData partial success:

                              we shouldn't change the existing behaviour before Qt 6 as it risks breaking existing code

                              We had cases where this happened before: @SGaist broke a lot my code but I still think changing it was the right choice

                              "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
                              1
                              • VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #15

                                Quick update, asked this question on the dev mailing list but no traction so far

                                "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

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

                                  Don't forget it might still be holiday season.

                                  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
                                  • VRoninV VRonin

                                    Quick update, asked this question on the dev mailing list but no traction so far

                                    kshegunovK Offline
                                    kshegunovK Offline
                                    kshegunov
                                    Moderators
                                    wrote on last edited by
                                    #17

                                    Yep, what @SGaist wrote. My advice is to give it a couple of days more, and if there's nothing try pinging the list.

                                    Read and abide by the Qt Code of Conduct

                                    VRoninV 1 Reply Last reply
                                    0
                                    • kshegunovK kshegunov

                                      Yep, what @SGaist wrote. My advice is to give it a couple of days more, and if there's nothing try pinging the list.

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

                                      @kshegunov said in QAbstractItemModel::setItemData partial success:

                                      try pinging the list

                                      I won't even try pinging it before September, don't worry. It was more to keep the 3 channels (forum/gerrit/mailing list) aware of each other

                                      "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
                                      0

                                      • Login

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