title: How can I get the background color returned by model take precedence over the style
body: <p>I have a <code>QTreeView</code> rendering <code>QAbstractItemModel</code>, where I would like to set the background of certain cells based on the data in the model. I return <code>QBrush</code> from <code>model::data(Qt::BackgroundColorRole)</code> and it works until I apply a style to an item.</p>
  
  <p>Setting any style to the item (even something that has nothing to do with background color, e.g. styling the border) overrides the color I return from the model (the calls to the model querying the background color are made). I.e. the view behaves as if the model never returns any color.</p>
  
  <p>I am using Qt 4.8 and I cannot upgrade to a later version.</p>
  
  <p>Is there a way to make the color returned from the model take precedence over the style?
  Why does the Qt behave in such a strange way - model has way more granularity and knows way more than a style can possibly know, why does the style take precedence - after all, the model doesn't have to return the color for every single cell - only a few specific ones?</p>
  
  <p>I assume it is a bug in Qt, I have opened a <a href="https://bugreports.qt.io/browse/QTBUG-46216" rel="nofollow noreferrer">bug report</a>, which is reproducible on this code:</p>
  
  <pre><code>#include &lt;QtCore/qabstractitemmodel.h&gt;
  #include &lt;QtGui/qtreeview.h&gt;
  #include &lt;QtGui/qtableview.h&gt;
  #include &lt;QtGui/qapplication.h&gt;
  
  class MyModel : public QAbstractItemModel
  {
  public:
      MyModel(QObject *parent) :QAbstractItemModel(parent){}
  
      int rowCount(const QModelIndex &amp;parent = QModelIndex()) const
      { return 2; }
      int columnCount(const QModelIndex &amp;parent = QModelIndex()) const
      { return parent.isValid() ? 0 : 2; }
      QVariant data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const
      {
          if(index.row() &gt;= 0 &amp;&amp; index.column() &gt;= 0)
          {
              switch(role)
              {
              case Qt::DisplayRole:
                  return QString("a");
              case Qt::BackgroundRole:
                  return QBrush(QColor(255 * index.row(), 255 * index.column(), 0));
              default:
                  break;
              }
          }
          return QVariant();
      }
      virtual QModelIndex index(int pos, int column, const QModelIndex &amp;parent = QModelIndex()) const
      { return createIndex(pos, column, 0); }
      virtual QModelIndex parent(const QModelIndex &amp;child) const
      { return QModelIndex(); }
  };
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      QTreeView view;
      MyModel myModel(0);
      view.setModel(&amp;myModel);
      view.show();
  
      //a.setStyleSheet("QTreeView::item { border: 1px solid black; }");
      return a.exec();
  }
  </code></pre>
  
  <p>If you uncomment the line before return, all the backgrounds are gone.</p>
  
  <p><img src="https://i.stack.imgur.com/wxub5.png" alt="This is what I get when the line is commented"></p>
  
  <p><img src="https://i.stack.imgur.com/pkO4h.png" alt="And this when I uncomment it"></p>