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. Force one cell in a QTableView to redraw

Force one cell in a QTableView to redraw

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.7k Views 1 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.
  • JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #1

    Does some UI-model-view expert know if it's possible/how to force just one cell of a QTableView to refresh/redraw/repaint?

    I have a QTableView bound to a model. I know perfectly well that normally a cell will get refreshed when the model emits a dataChanged signal in response to some role-value being changed. This is not my situation.

    I want to alter the color of (the text of) a cell on a timer. This is purely a UI-artefact, nothing to do with the model. I do not want to:

    • Update the model with a setData(index, colour, Qt::ForegroundRole).
    • I cannot get the model to execute a dataChanged() signal (even without an actual change), as that is a protected method of the model. [UPDATE Big correction, see later post.]
    • I do not wish to interpose a proxy model between the real model and the table view.
    • I do not wish to execute any kind of modelReset() nor complete QTableView refresh (too slow). It's only one cell which I want redrawn.

    I do have a QStyledItemDelegate on the view, that would have access to the color change I want, but I have the same problem of how can I convince the QTableView that I'd like it to redraw a cell?!

    It must be doable somehow! It may not be the same situation quite, but think want happens when you select a cell in a QTableView: it manages to redraw (just) that cell with a "selected-color" background, and it does not alter the underlying data source model.

    JonBJ 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #6

      I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around

      "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 2 Replies Last reply
      3
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        @JonB said in Force one cell in a QTableView to redraw:

        QStyledItemDelegate

        create a state on/off for your scenario. If on, draw a specific(index?) QStyledItemDelegate only and ignore painting of all other items?

        JonBJ 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          @JonB said in Force one cell in a QTableView to redraw:

          QStyledItemDelegate

          create a state on/off for your scenario. If on, draw a specific(index?) QStyledItemDelegate only and ignore painting of all other items?

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

          @JoeCFD
          Sorry Joe I have no idea what you're saying here. The problem is how to get QTableView to redraw a cell (whether it uses a delegate or not should be irrelevant) when nothing has changed in the bound model.

          JoeCFDJ 1 Reply Last reply
          0
          • JonBJ JonB

            @JoeCFD
            Sorry Joe I have no idea what you're saying here. The problem is how to get QTableView to redraw a cell (whether it uses a delegate or not should be irrelevant) when nothing has changed in the bound model.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by JoeCFD
            #4

            @JonB Sorry I thought you have some sort of delegates with override paint func. I had a look at the source code and did not find any flag to ignore painting of a cell. Can you try set flag Disable all other items to see if it helps. Here is a link to the source code and you may be able to dig something out for your problem.
            https://code.woboq.org/qt5/qtbase/src/widgets/itemviews/qtableview.cpp.html

            1 Reply Last reply
            0
            • JonBJ JonB

              Does some UI-model-view expert know if it's possible/how to force just one cell of a QTableView to refresh/redraw/repaint?

              I have a QTableView bound to a model. I know perfectly well that normally a cell will get refreshed when the model emits a dataChanged signal in response to some role-value being changed. This is not my situation.

              I want to alter the color of (the text of) a cell on a timer. This is purely a UI-artefact, nothing to do with the model. I do not want to:

              • Update the model with a setData(index, colour, Qt::ForegroundRole).
              • I cannot get the model to execute a dataChanged() signal (even without an actual change), as that is a protected method of the model. [UPDATE Big correction, see later post.]
              • I do not wish to interpose a proxy model between the real model and the table view.
              • I do not wish to execute any kind of modelReset() nor complete QTableView refresh (too slow). It's only one cell which I want redrawn.

              I do have a QStyledItemDelegate on the view, that would have access to the color change I want, but I have the same problem of how can I convince the QTableView that I'd like it to redraw a cell?!

              It must be doable somehow! It may not be the same situation quite, but think want happens when you select a cell in a QTableView: it manages to redraw (just) that cell with a "selected-color" background, and it does not alter the underlying data source model.

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

              @JonB said in Force one cell in a QTableView to redraw:

              I cannot get the model to execute a dataChanged() signal (even without an actual change), as that is a protected method of the model. [UPDATE Big correction, see later post.]

              Whoops! I mixed up virtual protected slot QAbstractItemView::dataChanged() with signal QAbstractItemModel::dataChanged().

              Anyway, if the model's dataChanged() is a signal, it's not terribly nice but my QTableView should be able to go

              emit model()->dataChanged(theCell);
              

              That will hopefully cause the QTableView to redraw my desired cell.

              I will try this out tomorrow....

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #6

                I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around

                "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 2 Replies Last reply
                3
                • VRoninV VRonin

                  I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around

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

                  @VRonin
                  Grrr, really?! Where have you been when I needed you!? None of the experts knew, I knew you'd know :) That sounds much neater (than the signal I had to as per above), thanks! :)

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around

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

                    @VRonin
                    I have now had a chance to try out QAbstractItemView::update(QModelIndex), and am pleased to confirm it does exactly what I wanted!

                    Thank you for drawing my attention to this method. I could not find it? Why? Because of the irritating way Qt chooses to arrange methods in its documentation....

                    I started by going to https://doc.qt.io/qt-5/qtableview.html page. No update() methods there. So I clicked the List of all members, including inherited members at the top of the page. There I scroll through the big list alphabetically, till I come across the adjacent group of entries:

                    update(int , int , int , int )
                    update(const QRect &)
                    update(const QRegion &)
                    

                    Nothing using a QModelIndex. So I gave up.

                    What I didn't notice is that right up near the start of the whole list is the update(const QModelIndex &) entry. And why is it there, miles away from the other ones in alphabetical order? Answer: because it happens to be a Public Slot. And Qt docs, in its infinite wisdom, thinks it's a good idea to produce alphabetical lists all mixed in together but with things like slots placed separately from Public Functions etc. in the alphabetical list.... When if a human is looking for an item in a list this is not what they expect. Grrrr!

                    This is not the first time I have been deceived by the alphabetical list of functions re-starting the alphabetical list a couple of times for different "categories" of functions. It's really annoying.... :(

                    Thanks anyway. Would you care to rearrange the Qt docs for this issue? ;-)

                    1 Reply Last reply
                    2

                    • Login

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