I've found a way of doing this trough a delegate. Here is the code
class TextColorDelegate: public QItemDelegate
{
public:
explicit TextColorDelegate(QObject* parent = 0) : QItemDelegate(parent)
{ }
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyleOptionViewItem ViewOption(option);
QColor itemForegroundColor = index.data(Qt::ForegroundRole).value<QColor>();
if (itemForegroundColor.isValid())
{
if (itemForegroundColor != option.palette.color(QPalette::WindowText))
ViewOption.palette.setColor(QPalette::HighlightedText, itemForegroundColor);
}
QItemDelegate::paint(painter, ViewOption, index);
}
};
And for using delegate you should write something like this
pTable->setItemDelegate(new TextColorDelegate(this));
where pTable
's type is QTableView*
;