0

I have custom model that return necessary foreground color for the ForegroundRole:

QVariant AlertTreeModel::data(const QModelIndex& index, int role) const
{
    if (!index.isValid())
    {
        return {};
    }

    const auto item = itemForIndex(index);
    switch (role)
    {    
        case Qt::ForegroundRole:
            return item->color(); // return different colors for the different item states

        default:
            return {};
    }
}

View (bit simplified code):

void UniformIndentationTreeView::drawRow(QPainter* painter, const QStyleOptionViewItem& options, const QModelIndex& index) const
{
    QStyleOptionViewItem opt = options;
    QTreeView::drawRow(painter, opt, index);
}

I also have the simple custom delegate where I can ensure I get correct foreground color:

void AlertTreeViewItemDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const
{
    UniformIndentationItemViewDelegate::initStyleOption(option, index);

    QColor color = index.data(Qt::ForegroundRole).value<QColor>();

    INFOLOG << "tree item color = " << color.name(QColor::HexRgb);

    option->palette.setColor(QPalette::WindowText, color);
    option->palette.setColor(QPalette::HighlightedText, color);
}

The problem is that my tree is painted with colors from a style sheet and doesn't use the colors which the model returns. How to force view/delegate to use custom foreground color?

Update. In the Qt sources we can see that QStyledItemDelegate::paint take widget's style to draw controls.

void QStyledItemDelegate::paint(QPainter *painter,
        const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_ASSERT(index.isValid());
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);
    const QWidget *widget = QStyledItemDelegatePrivate::widget(option);
    QStyle *style = widget ? widget->style() : QApplication::style();
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
}

How to draw items with colors from a model, not from a style sheet?

Vladimir Bershov
  • 2,701
  • 2
  • 21
  • 51
  • Style sheets always win - properly described in the [docs](https://doc.qt.io/qt-5/stylesheet.html): "Style sheets are applied on top of the current widget style" – chehrlic Mar 03 '22 at 16:30
  • If you want to 'bypass' the style then just have your delegate inherit from `QItemDelegate` rather than `QStyledItemDelegate`. As far as I recall that should do pretty much what you want. – G.M. Mar 03 '22 at 16:51

0 Answers0