title: QFontMetrics returns inaccurate results
body: <p>I have a custom delegate in my <code>QTableWidget</code> to hightlight matches if a user searches something. Unfortunately the rectangle position does often not really fit This happens on some characters or phrases or depending on the number of matches or the size of the leading string. I can't find something specific causing this. Here is one example: <img src="https://i.stack.imgur.com/GTPLv.png" alt="Example">. </p>
  
  <p>This is my paint routine (a bit messy from all the trial and error trying to fix the issue):</p>
  
  <pre><code>void custom_delegate::paint(QPainter* painter, const QStyleOptionViewItem&amp; option, const QModelIndex&amp; index) const{
  
      const QTableWidget* table_widget = qobject_cast&lt;const QTableWidget*&gt;(qstyleoption_cast&lt;const QStyleOptionViewItemV3*&gt;(&amp;option)-&gt;widget);
      const int cell_width = table_widget-&gt;columnWidth(index.column());
  
      // basic table cell rectangle
      QRect rect_a = option.rect;
  
      // adjust rectangle to match text begin
      QStyle* style;
      if(table_widget != 0){
          style = table_widget-&gt;style();
      }else{
          style = QApplication::style();
      }
      const int text_horizontal_margin = style-&gt;pixelMetric(QStyle::PM_FocusFrameHMargin, 0, table_widget) + 1;
      QRect rect_b = rect_a.adjusted(text_horizontal_margin, 0, -text_horizontal_margin, 0);
  
      // adjust rectangle to match text height
      QFont cell_font = index.model()-&gt;data(index, Qt::FontRole).value&lt;QFont&gt;();
      cell_font.setPointSize(9);
      QFontMetrics fm(cell_font);
      const int height = fm.height();
  
      rect_b.setY(rect_a.y() + (rect_a.height() - height)/2);
      rect_b.setHeight(height);
  
      // displayed text
      std::string cell_text = qstrtostr(fm.elidedText(index.model()-&gt;data(index, Qt::DisplayRole).toString(),Qt::ElideRight,rect_a.width()));
      int found_pos = find_ci(cell_text, this-&gt;filter_string, 0);
      int old_pos = 0;
      int found_width = 0;
      QRect rect_c = rect_b;
  
      // find occurence of filter string in cell_text
      while(found_pos != std::string::npos){
  
          std::string front = cell_text.substr(0, found_pos);
          rect_c.setX(rect_b.x() + fm.tightBoundingRect(QString::fromStdString(front)).width());
          rect_c.setWidth(fm.width(QString::fromStdString(cell_text.substr(found_pos, this-&gt;filter_string.size()))));
          painter-&gt;fillRect(rect_c, Qt::yellow);
          old_pos = found_pos+1;
          found_pos = find_ci(cell_text, this-&gt;filter_string, old_pos);
      }
  }
  </code></pre>
  
  <p><strong>Notes:</strong> <code>filter_string</code> is the string searched for, <code>find_ci</code> is just a wrapper for <code>std::string::find</code> including case-insensitivity but not important here as this test case is fully lower-case and I use <code>std::string</code> for non-qt stuff.</p>
  
  <p><strong>Edit:</strong> For the width calculation I tried <code>fm.tightBoundingRect().width()</code>, <code>fm.boundingRect.width()</code> and <code>fm.width()</code> with different but never correct results.</p>
  
  <p>I use Qt 5.2</p>