QTableview row color vanishes when focus lost
-
Hi,
I have a model (ClientsModel
) and a proxymodel (ClientsProxyModel
) for it., The proxy should take care of some coloring.
It does so by querying another model and asking for the latest entry's timestamp.
If the entry was done within the last hour, the row color is set to "green".QVariant ClientsProxyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); //ForegroundRole or BackgroundRole if(role == Qt::ForegroundRole) { QModelIndex idxClient = sourceModel()->index(index.row(), int(ClientsModel::Column::ID)); int clientId = sourceModel()->data(idxClient).toInt(); QDateTime lastTS = anotherModel->getLastEntryTS(clientId); QDateTime now = QDateTime::currentDateTime(); now = now.addSecs(-3600); if(lastTS > now) { return QBrush(QColor(55, 200, 40, 255)); //ff c9 2a } else { return {}; } } else { return(sourceModel()->data(index, role)); } return {}; }
It works, but unfortunately the moment the mouse cursor leaves the tableview widget colors are reset to defaults.
Is there a differentQt::xyzRole
I have to look into or do I have to change some (tableview) settings to fix this?Kind regards,
schotter -
Hi,
I have a model (ClientsModel
) and a proxymodel (ClientsProxyModel
) for it., The proxy should take care of some coloring.
It does so by querying another model and asking for the latest entry's timestamp.
If the entry was done within the last hour, the row color is set to "green".QVariant ClientsProxyModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); //ForegroundRole or BackgroundRole if(role == Qt::ForegroundRole) { QModelIndex idxClient = sourceModel()->index(index.row(), int(ClientsModel::Column::ID)); int clientId = sourceModel()->data(idxClient).toInt(); QDateTime lastTS = anotherModel->getLastEntryTS(clientId); QDateTime now = QDateTime::currentDateTime(); now = now.addSecs(-3600); if(lastTS > now) { return QBrush(QColor(55, 200, 40, 255)); //ff c9 2a } else { return {}; } } else { return(sourceModel()->data(index, role)); } return {}; }
It works, but unfortunately the moment the mouse cursor leaves the tableview widget colors are reset to defaults.
Is there a differentQt::xyzRole
I have to look into or do I have to change some (tableview) settings to fix this?Kind regards,
schotter@schotter
To me this code looks fine. You could try in a standalone program, to rule out any CSS or selection/focus stuff your code could have added.If color changes when "the mouse cursor leaves the tableview widget" that sounds odd. If it's to do with colors when focussed/unfocussed what platform/window manager are you running under?
You could just try something with CSS only to see how that behaves?
-
@schotter
To me this code looks fine. You could try in a standalone program, to rule out any CSS or selection/focus stuff your code could have added.If color changes when "the mouse cursor leaves the tableview widget" that sounds odd. If it's to do with colors when focussed/unfocussed what platform/window manager are you running under?
You could just try something with CSS only to see how that behaves?
@JonB thanks for your input :)
I found the source of my problem. It's how I handle my data in the model.
The data behindanotherModel->getLastEntryTS(clientId);
changes whenever a client in the same tableview is selected. It basically only contains the selected client's data. So when the cursor is moved to a different row (another client), the old "green" row stays "green", because the view is not updated for that old row. But when the cursor moves back, the data for the "old" client isn't present anymore and therefore I can't read a correct timestamp from the model.
Maybe my mistake helps someone else but I have my doubts^^ -