title: Correct highlighting with qt custom delegates
body: <p>I am making a table control that displays some additional text data apart from those in DisplayRole of its model. In all other respects text and cell display should be identical. What i am having trouble with is correct display of highlighted cell.</p>
  
  <p>I am currently using the following code:</p>
  
  <pre><code>void MatchDelegate::paint(QPainter *painter, const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const
  {
  
   if (option.state &amp; QStyle::State_Selected)
        painter-&gt;fillRect(option.rect, option.palette.highlight());
   painter-&gt;save();
   QString str = qvariant_cast&lt;QString&gt;(index.data())+ "\n";
   str  += QString::number(qvariant_cast&lt;float&gt;(index.data(Qt::UserRole)));
   if (option.state &amp; QStyle::State_Selected)
       painter-&gt;setBrush(option.palette.highlightedText());
   else
       painter-&gt;setBrush(qvariant_cast&lt;QBrush&gt;(index.data(Qt::ForegroundRole)));
  
   painter-&gt;drawText(option.rect, qvariant_cast&lt;int&gt;(index.data(Qt::TextAlignmentRole)), str);
   painter-&gt;restore();
  
  }
  </code></pre>
  
  <p>However, the result looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/q10W3.png" alt="highlighted cell"></p>
  
  <p>Text color is wrong, there is no dashing line around the cell, and when the control loses focus, the cell remains blue instead of becoming light gray like drawn by default cells do.</p>
  
  <p>How should painting code be changed to fix those issues?</p>