Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default selection color for QTableView

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?

like image 916
Yousuf Azad Avatar asked Feb 02 '26 16:02

Yousuf Azad


1 Answers

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>();
like image 71
Tomas Avatar answered Feb 05 '26 08:02

Tomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!