1
votes

I am using a QTableView with QAbstractTableModel.

I have subclassed QAbstractTableModel and in my model class for Qt::BackgroundRole and Qt::ForegroundRole I have returned some color depending on some condition.

And I have subclassed QTableView class.

When I select any row/cell on the table, the row/cell is highlighted in the default color of the tabelVeiw, it does not show the color returned from my model class.

How can I change this behavior? How can I avoid/ignore this default coloring of QTableView and have only the colors returned by my model class?

1
Any code samples? And why you need to subclass QTableView? - Dmitry Sazonov
I subclassed QTableView because in my case the table will react to left click, double click, and right click. Without subclassing, with the given signal slots if I implement QWidget::customContextMenuRequested and QAbstractItemView::clicked then if I right click it emits the click() signal as well. That is why I had to subclass QTableView and re-implement mousePressEvent to differentiate left and right mouse click - Yousuf Azad
You should use QItemDelegate to handle mouse events. - Dmitry Sazonov
Can you please take the time to elaborate please? :) I want only the QColor/QBrush that is returned from my model class to represent my TableView cells and nothing else. How can I do this? Your elaborate answer will be much appreciated :) - Yousuf Azad
Stackoverflow is not designed for free code writing. Once again - you need to clarify your question and provide an SSCCE. You may start with Qt documentation and ask extra questions if something will not be clear. - Dmitry Sazonov

1 Answers

2
votes

You have to use a custom delegate. Subclass the QStyledItemDelegate and implement it's paint() method like this:

void MyDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
    QStyleOptionViewItem itemOption(option);              
    initStyleOption(&itemOption, index); 

    if ((itemOption.state & QStyle::State_Selected) &&
        (itemOption.state & QStyle::State_Active))
        itemOption.palette.setColor(QPalette::Highlight, Qt::red);  // set your color here

    QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter, nullptr);
}

If you want to get yout selecion color from the model, I suggest to define a special custom role for that purpose:

enum MyRoles
{
    HighlightColorRole = Qt::UserRole
};

Your model will use this role to return your custom highlight (selection) color through the QAbstractItemModel::data() method.

In your delegate you can obtain that color like this:

QColor color = index.data(HighlightColorRole).value<QColor>();