What is the right way to color-code rows in a QTableView
?
I'm developing a spreadsheet application that should color-code its rows based on a specific value set in one of the columns. I use QSqlRelationalTableModel
and QSqlRelationalDelegate
; because, the value that should determine the color is a foreign key.
Why can't it be as simple as the following? Any ideas?
model->setData( model->index( index.row(), index.column() ),
QBrush(Qt::red),
Qt::BackgroundRole );
You should overwrite the data function of QSqlRelationalTableModel and when you get the Qt :: BackgroundRole role to filter according to your case and return the appropriate QBrush, in the following example filter by the foreign field and check that it is equal to Lima:
Example:
sqlrelationaltablemodel.h
sqlrelationaltablemodel.cpp
Output:
The complete example can be found here.