title: Styling Qt Model/View items based on their ItemDataRole properties
body: <p>I have a <code>QTreeView</code> with items from my own model (<code>QAbstractItemModel</code> derived). Now I would like to style the items (with a stylesheet on the <code>QTreeView</code>) so that depending on various properties they would have various backgrounds.</p>
  
  <p>Also it seems that this problem applies to any Model/View containers. Not just <code>QTreeView</code>.</p>
  
  <hr>
  
  <p>For example doing alternate backgrounds is easy. <code>alternatingRowColors</code> on the <code>QTreeView</code> has to be <code>true</code>. Then in stylesheet you may use <code>:alternate</code> pseudo-selector to change properties for the alternate items so:</p>
  
  <pre><code>QTreeView::item {
    background: green;
  }
  
  QTreeView::item:alternate {
    background: blue;
  }
  </code></pre>
  
  <hr>
  
  <p>Doing selected items is similarly simple. In fact doing anything that is handled by Qt's pseudo-selectors is easy.</p>
  
  <p>But what about properties that are not pseudo-selectors? In particular how to select style based on values associated with <code>ItemDataRole</code> (which seams to be the only reasonably generic method of storing data in models)?</p>
  
  <p><em>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.</em></p>
  
  <hr>
  
  <p>With <code>QTreeWidget</code> it seems you can go by having a dedicated <code>QWidget</code>-derived class for the items where you would add <code>Q_PROPERTY</code> with whatever you want. Then you can access that property by name in the stylesheet.</p>
  
  <p>See for example <a href="http://www.qtcentre.org/threads/30095-Using-custom-Q_PROPERTY-with-Stylesheet" rel="nofollow">"Using custom Q_PROPERTY with Stylesheet" question on Qt Centre</a> and <a href="http://qt-project.org/doc/qt-4.7/stylesheet-examples.html" rel="nofollow">"Trigger an update for the widget while using dynamic properties." note at the end of Qt Style Sheets Examples</a>.</p>
  
  <p>But the <code>*View</code> classes do not have any <code>QWidget</code>s or even <code>QObject</code>s for their items (or at least not public accessible ones).</p>
  
  <hr>
  
  <p>There is also possibility to provide own <code>QAbstractItemDelegate</code>, possibly one derived from <code>QStyledItemDelegate</code>. But that makes stylesheet interaction a bit harder.</p>
  
  <p>With simple staff like <code>background-color</code> property it seems doable. But how would you simulate <code>border-image</code>, <code>padding</code> and many other styles in complex sets?</p>
  
  <p>Not to mention that once you write an item delegate the code is frozen. If <code>Qt</code> 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...</p>
  
  <hr>
  
  <p>So is there any way to subclass in stylesheet based on model-provided properties/data?</p>