0

My problem is very similar to this question.

I have a QTreeView which is connected to a model. Is it possible to style each level of the tree in different way? I need to change background color of each line. Different level would have a different background. Expanded items would have a different color as well.

I have already tried this http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-qtreeview It seems that it is not possible to do so using only css. Could QProxyStyle help? How would it be used?

Thank you, Martin.

Community
  • 1
  • 1
MPeli
  • 570
  • 1
  • 8
  • 19

1 Answers1

0

If you are using a custom model (derived from QAbstractItemModel or similar), you can achieve this by returning an appropriate value from QAbstractItemModel::data() when data for any appearance role is requested:

QVariant MyModel::data(const QModelIndex& index, int role) const
{
    switch(role)
    {
        case Qt::DisplayRole:
        {
            ...
            break;
        }
        case Qt::BackgroundRole:
            return QColor(Qt::red);
        case ...
    }

    return QVariant();
}

Using QStandardModel you can set data for these roles with QStandardItem::setData().

Murphy
  • 3,827
  • 4
  • 21
  • 35