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::dataChanged what would you prefer?
Forum Updated to NodeBB v4.3 + New Features

QAbstractItemModel::dataChanged what would you prefer?

Scheduled Pinned Locked Moved Solved Brainstorm
9 Posts 6 Posters 3.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

    Flavour question about QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles):

    Say you have a model that changes, at the same time, the Qt::DisplayRole of the index(0,0) and Qt::FontRole of the index(1,0)

    Would you prefer that the model emitted dataChanged(index(0,0),index(1,0),{Qt::DisplayRole,Qt::FontRole}); or would you rather have dataChanged(index(0,0),index(0,0),{Qt::DisplayRole}); dataChanged(index(1,0),index(1,0),{Qt::FontRole});?

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

      Flavour question about QAbstractItemModel::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles):

      Say you have a model that changes, at the same time, the Qt::DisplayRole of the index(0,0) and Qt::FontRole of the index(1,0)

      Would you prefer that the model emitted dataChanged(index(0,0),index(1,0),{Qt::DisplayRole,Qt::FontRole}); or would you rather have dataChanged(index(0,0),index(0,0),{Qt::DisplayRole}); dataChanged(index(1,0),index(1,0),{Qt::FontRole});?

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

      Me, flavor 2, especially if Qt::UserRole isn't anything that should be updated in the GUI.

      Read and abide by the Qt Code of Conduct

      VRoninV 1 Reply Last reply
      1
      • kshegunovK kshegunov

        Me, flavor 2, especially if Qt::UserRole isn't anything that should be updated in the GUI.

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

        @kshegunov said in QAbstractItemModel::dataChanged what would you prefer?:

        if Qt::UserRole isn't anything that should be updated in the GUI.

        You are right, that aspect is important, changed the question so that both roles matter for the UI

        "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
          #4

          Hi,

          Also number two as it might optimise the number of calls to data.

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

            @kshegunov said in QAbstractItemModel::dataChanged what would you prefer?:

            if Qt::UserRole isn't anything that should be updated in the GUI.

            You are right, that aspect is important, changed the question so that both roles matter for the UI

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

            I probably would stick to flavor 2, even though performance-wise shouldn't much matter. On the other hand for anything more "uncountable" (as 4, 5+ model indexes) then I'd switch to flavor 1.

            Read and abide by the Qt Code of Conduct

            JKSHJ 1 Reply Last reply
            2
            • kshegunovK kshegunov

              I probably would stick to flavor 2, even though performance-wise shouldn't much matter. On the other hand for anything more "uncountable" (as 4, 5+ model indexes) then I'd switch to flavor 1.

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

              I'd generally prefer flavour 2 too. It's more "correct", since index(0, 0) hasn't changed its FontRole and index(1, 0) hasn't changed its DisplayRole.

              In the end though, I'd also consider the code complexity and impact. If this was a custom model within my own app, I'd just choose the option that requires simpler code (which I think is Flavour 1).

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

              VRoninV 1 Reply Last reply
              2
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                When you look at the default implementation you will see that the second one is better:
                https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qabstractitemview.cpp.html#_ZN17QAbstractItemView11dataChangedERK11QModelIndexS2_RK7QVectorIiE

                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
                5
                • J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  I would go with the 2nd option, simply for the fact that it's much clearer, what is supposed to happen. Or rather what just happend.


                  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
                  1
                  • JKSHJ JKSH

                    I'd generally prefer flavour 2 too. It's more "correct", since index(0, 0) hasn't changed its FontRole and index(1, 0) hasn't changed its DisplayRole.

                    In the end though, I'd also consider the code complexity and impact. If this was a custom model within my own app, I'd just choose the option that requires simpler code (which I think is Flavour 1).

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

                    @JKSH said in QAbstractItemModel::dataChanged what would you prefer?:

                    I'd just choose the option that requires simpler code (which I think is Flavour 1)

                    it's actually flavour 2 in my case as I would have to check that the two indexes are one next to the other while I don't care using flavour 2

                    @Christian-Ehrlicher said in QAbstractItemModel::dataChanged what would you prefer?:

                    When you look at the default implementation you will see that the second one is better:

                    Ok, this is a show stopper. I did not realise sending topLeft!=bottomRight (often) triggers a repaint of the entire view. This makes flavour 2 strictly better

                    Thanks everyone for your insight

                    "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