[Interest] Styling a single QTreeWidgetItem

Stephen Bryant steve at bawue.de
Thu Mar 7 17:07:40 CET 2013


On 06/03/2013 16:54, André Somers wrote:
> Op 6-3-2013 16:19, Sensei schreef:
>> I don't know if this is possible, but how can I style a single
>> QTreeWidgetItem?
>>
[...]
> Use a custom delegate.
> libQxt contains QxtItemDelegate[1] that styles the root nodes in trees 
> in a different way. That could work as inspiration even if you don't use 
> it directly.

While that is a way of solving the problem, it seems a bit overkill for
someone who only wants to style items in one of the standard view types.

I wanted to do something similar, and I've been looking for a better way
to do it.  I'm currently using various roles (eg: font, background et
al) in my item model's data() method.  It works, but then you have to
start messing about with extra logic for things like alternate rows and
so on, which may end up being wrong for the view anyway!  CSS would be
much cleaner.

It also means duplicate configuration.  I haven't seen a way of querying
the style sheet for values.  Is there one?


> I know of no way to do this using style sheets.

You'd have to get from the model to the actual items in the view to be
able to mark them.  There's no way to do this, as the model doesn't know
about the view(s).  It's the view that queries the model.

One possible way to do so would be to add a new role with which
properties could be set - eg. a Qt::PropertyRole which uses the type
QMap<QString, QVariant>.  If the standard views would use that role to
set properties on their items, we could then match those in the style sheet.

QTreeView#myTreeView::item[prop1="foo"] { background-color: yellow }


I suppose I should submit a feature request for this.
Where do I do that?

Steve



More information about the Interest mailing list