3

I have a QTreeView with items from my own model (QAbstractItemModel derived). Now I would like to style the items (with a stylesheet on the QTreeView) so that depending on various properties they would have various backgrounds.

Also it seems that this problem applies to any Model/View containers. Not just QTreeView.


For example doing alternate backgrounds is easy. alternatingRowColors on the QTreeView has to be true. Then in stylesheet you may use :alternate pseudo-selector to change properties for the alternate items so:

QTreeView::item {
  background: green;
}

QTreeView::item:alternate {
  background: blue;
}

Doing selected items is similarly simple. In fact doing anything that is handled by Qt's pseudo-selectors is easy.

But what about properties that are not pseudo-selectors? In particular how to select style based on values associated with ItemDataRole (which seams to be the only reasonably generic method of storing data in models)?

Note that there is a trick. If for example you do not use checked state you might "reuse" that state to map to something of your own and then you have a pseudo-selector for it... But this is just a workaround and not even a general one.


With QTreeWidget it seems you can go by having a dedicated QWidget-derived class for the items where you would add Q_PROPERTY with whatever you want. Then you can access that property by name in the stylesheet.

See for example "Using custom Q_PROPERTY with Stylesheet" question on Qt Centre and "Trigger an update for the widget while using dynamic properties." note at the end of Qt Style Sheets Examples.

But the *View classes do not have any QWidgets or even QObjects for their items (or at least not public accessible ones).


There is also possibility to provide own QAbstractItemDelegate, possibly one derived from QStyledItemDelegate. But that makes stylesheet interaction a bit harder.

With simple staff like background-color property it seems doable. But how would you simulate border-image, padding and many other styles in complex sets?

Not to mention that once you write an item delegate the code is frozen. If Qt changes ways of rendering stylesheet (improves it somehow) you will not benefit from it... Also is makes logic/presentation separation a bit harder. Now you need a programmer to style those items, not just a CSS-enabled graphics-guy...


So is there any way to subclass in stylesheet based on model-provided properties/data?

Adam Badura
  • 5,069
  • 1
  • 35
  • 70
  • I found a similar question on a different forum (http://lists.qt.nokia.com/public/qt-interest/2010-May/023771.html). But it is a bit old now and with little activity so I thought about asking it here again. Hoping for more constructive or knowledgeable answers. – Adam Badura Feb 01 '13 at 08:27
  • Another question trying to achieve similar effect: http://stackoverflow.com/questions/1982351/using-multiple-qstyleditemdelegate-with-stylesheets with a negative answer. – Adam Badura Feb 01 '13 at 20:54
  • 1
    Due to lack of activity here I asked the question on Qt Centre (see http://www.qtcentre.org/threads/53210-Styling-Model-View-items-based-on-their-ItemDataRole-properties) and got a negative reply (cannot be done) from "wysota". – Adam Badura Feb 08 '13 at 10:29
  • 1
    Sad story, and sadly I'm also facing this problem now... Thank you for the follow up story. – Surya Wijaya Madjid Nov 12 '14 at 15:53
  • Is the answer that you cannot do it, that you would need to use a custom delegate for this? – eric Jan 11 '15 at 19:17
  • @neuronet Yes, it is so. – Adam Badura Jan 12 '15 at 09:56
  • @AdamBadura if you wrote an answer to your question it might help some of us... :) – eric Jan 14 '15 at 04:00
  • The link http://qt-project.org/doc/qt-4.7/stylesheet-examples.html is dead. Maybe http://doc.qt.io/qt-4.8/stylesheet-examples.html#customizing-using-dynamic-properties still contains what you referred to? – equaeghe Nov 19 '15 at 09:40
  • @equaeghe It looks as if that what you provide could be a solution. Although is anything but `bool` supported? Since examples show only `bool`. However now I'm long time in a different project, no Qt anymore. So it is hard to check. – Adam Badura Nov 19 '15 at 09:48
  • @AdamBadura I was just pointing out a dead link... I'm not very knowledgeable about Qt. – equaeghe Nov 19 '15 at 13:41

0 Answers0