In order to change the color of the items in the model you need to reimplement the data function and return the colors you want for theQt::BackgroundColorRole or the Qt::TextColorRole roles.
See the documentation:
The example below demonstrates how this can be achieved.
Note that you can also reimplement the QItemDelegate::paint() in order to change the color the items in a view. See the following FAQ:
#include
class ColorModel : public QAbstractTableModel
{
public:
ColorModel(QObject *parent)
{
QStringList firstRow;
QStringList secondRow;
for (int i = 0; i < 5; i++ ) {
firstRow.insert(i,"Row " + QString::number(i+1));
}
for (int i = 0; i < 5; i++ ) {
secondRow.insert(i,"Row " + QString::number(i+1));
}