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
#ifndef SQLRELATIONALTABLEMODEL_H
#define SQLRELATIONALTABLEMODEL_H
#include <QSqlRelationalTableModel>
class SqlRelationalTableModel : public QSqlRelationalTableModel
{
Q_OBJECT
public:
SqlRelationalTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase());
QVariant data(const QModelIndex & item, int role = Qt::DisplayRole) const;
};
#endif // SQLRELATIONALTABLEMODEL_H
sqlrelationaltablemodel.cpp
#include "sqlrelationaltablemodel.h"
#include <QBrush>
SqlRelationalTableModel::SqlRelationalTableModel(QObject *parent, QSqlDatabase db)
:QSqlRelationalTableModel(parent, db)
{
}
QVariant SqlRelationalTableModel::data(const QModelIndex &item, int role) const
{
if(role == Qt::BackgroundRole)
if(QSqlRelationalTableModel::data(index(item.row(), 2), Qt::DisplayRole).toString().trimmed() == "Lima")
return QVariant(QBrush(Qt::red));
return QSqlRelationalTableModel::data(item, role);
}
Output:

The complete example can be found here.