PDA

View Full Version : How to change the cell text color in a QTableView?



vrltwe
24th July 2013, 18:19
I've tried this (without success):



ui->tableView->model()->setData(ui->tableView->model()->index(5, 5, QModelIndex()), QVariant(QColor(Qt::red)), Qt::DecorationRole);

Thanks in advance.

ChrisW67
24th July 2013, 22:41
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
25th July 2013, 01:21
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.

ChrisW67
25th July 2013, 03:56
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
25th July 2013, 04:55
A QSqlTableModel is beeing used.

ChrisW67
25th July 2013, 05:57
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
25th July 2013, 16:58
I made some progress by means of subclassing QSqlTableModel and extending data() function.


QVariant MySqlTableModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlTableModel::data(index, role);
if (role == Qt::TextColorRole && index.column() == 1)
{
// double d = value.toDouble();
// if(d > 0)
return QVariant::fromValue(QColor(Qt::blue));
}
return value;


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


double d = this->record(index.row()).value(index.column()).toDouble ();

ChrisW67
25th July 2013, 22:47
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