I've tried this (without success):
Qt Code:
ui->tableView->model()->setData(ui->tableView->model()->index(5, 5, QModelIndex()), QVariant(QColor(Qt::red)), Qt::DecorationRole);To copy to clipboard, switch view to plain text mode
Thanks in advance.
I've tried this (without success):
Qt Code:
ui->tableView->model()->setData(ui->tableView->model()->index(5, 5, QModelIndex()), QVariant(QColor(Qt::red)), Qt::DecorationRole);To copy to clipboard, switch view to plain text mode
Thanks in advance.
Qt::ForegroundRole
Setting it with setData() is only useful if the model has somewhere to store the value. A QStandardItemModel does, for example, but a base QSqlTableModel does not.
vrltwe (26th July 2013)
Hi ChrisW67,
Thank you for your answer.
But I still have no idea how to proceed in order to set the color of some cells. Could you please provide further information.
Thanks in advance.
You are using a QTableView. Colours in that view are driven by the data in the model (or alternate behaviour implemented in a QStyledItemDelegate). You need to arrange for the underlying model to return the desired text colour when queried for the Qt::ForegroundRole. Without more information in the model you are actually using there not much more we can say.
vrltwe (26th July 2013)
A QSqlTableModel is beeing used.
QSqlTableModel does not return a value for Qt::ForegroundRole.
You need to create a sub-class of QSqlTableModel with an extended data() function that returns something for Qt::ForegroundRole.
Alternatively, place a QIdentityProxyModel subclass between the QSqlTableModel and the view and put the extended data() function in the proxy.
Alternatively, write a QStyledItemDelegate sub-class that paints the cells differently (hardest to get right).
Which option you choose depends on how many views are watching the model, whether the colour difference is unique to one view, etc.
vrltwe (26th July 2013)
I made some progress by means of subclassing QSqlTableModel and extending data() function.
Qt Code:
{ if (role == Qt::TextColorRole && index.column() == 1) { // double d = value.toDouble(); // if(d > 0) } return value;To copy to clipboard, switch view to plain text mode
After that, I tried to compare value with something else, which will led to paint or not the text. But it doesn't work (commented lines). May I ask some orientation on this issue?
Also I am wondering about the benefits of the alternative approaches.
Thanks for the valuable answers.
Added after 29 minutes:
It seems that the value could be retrieved with
Qt Code:
double d = this->record(index.row()).value(index.column()).toDouble();To copy to clipboard, switch view to plain text mode
Last edited by vrltwe; 25th July 2013 at 16:58.
You have the value that the model would normally return for the ForegoundRole, a null QVariant, not the value it would normally return for DisplayRole or EditRole. Comparing that to zero is not going to be useful. You can get the value the way you have, or using data() on the same index but Qt::EditRole inside the if bolck
vrltwe (26th July 2013)
Bookmarks