Force one cell in a QTableView to redraw
-
Does some UI-model-view expert know if it's possible/how to force just one cell of a
QTableViewto refresh/redraw/repaint?I have a
QTableViewbound to a model. I know perfectly well that normally a cell will get refreshed when the model emits adataChangedsignal 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 aprotectedmethod 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 completeQTableViewrefresh (too slow). It's only one cell which I want redrawn.
I do have a
QStyledItemDelegateon the view, that would have access to the color change I want, but I have the same problem of how can I convince theQTableViewthat 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. - Update the model with a
-
I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around
-
@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?
-
@JoeCFD
Sorry Joe I have no idea what you're saying here. The problem is how to getQTableViewto redraw a cell (whether it uses a delegate or not should be irrelevant) when nothing has changed in the bound model.@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 -
Does some UI-model-view expert know if it's possible/how to force just one cell of a
QTableViewto refresh/redraw/repaint?I have a
QTableViewbound to a model. I know perfectly well that normally a cell will get refreshed when the model emits adataChangedsignal 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 aprotectedmethod 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 completeQTableViewrefresh (too slow). It's only one cell which I want redrawn.
I do have a
QStyledItemDelegateon the view, that would have access to the color change I want, but I have the same problem of how can I convince theQTableViewthat 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.@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()withsignal QAbstractItemModel::dataChanged().Anyway, if the model's
dataChanged()is a signal, it's not terribly nice but myQTableViewshould be able to goemit model()->dataChanged(theCell);That will hopefully cause the
QTableViewto redraw my desired cell.I will try this out tomorrow....
- Update the model with a
-
I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around
-
I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around
-
I'm late but you can just call QAbstractItemView::update(QModelIndex) passing the index you want to repaint, no need to hack around
@VRonin
I have now had a chance to try outQAbstractItemView::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? ;-)