title: Set QGraphicsTextItem text contents of exact height and width
body: <p>I am required to create text items with <strong>exact width and height of text contents</strong>.<br>
  The height of the text is the most important requirement.<br>
  The position of the text should be relative to the text itself.</p>
  
  <p>I also have to be able to place it on canvas in an exact spot.</p>
  
  <p>Assuming a (printable) canvas (on a larger <code>QGraphicsScene</code>), say 5 inch width and 1 inch height, my text should be able to stretch top-bottom-left-right - and be placed on canvas, not part in part out.</p>
  
  <hr>
  
  <p>I am sub-classing <code>QGraphicsTextItem</code> for my item type. I am resizing it, using <code>QTransform()</code>, to required size - in inches or mm or pixels (72*in).<br>
  Also setting the <code>document()</code> margin to 0, and anything inside (like <code>QTextBlockFormat</code> margins) also to 0. </p>
  
  <p>I have implemented a <code>setItemSize(QSizeF sz)</code> (with sz in pixels), that resizes the <code>QGraphicsTextItem</code> as required.<br>
  The sz is initialized using the item bounding rect.<br>
  Assuming no wrap, single line text (multi-line could be solved separately once this issue is resolved).</p>
  
  <p>When adding the item to canvas, I still see a top and bottom margin - and this varies based on font choice.<br>
  I drew a rectangle around the item to see it.<br>
  The top/bottom distances depend on font choices.</p>
  
  <hr>
  
  <p>I have tried to use font metrics to determine these distances (in <code>paint()</code> I have been drawing lines to try to determine the position and rectangle in which the text fits).</p>
  
  <p>I would be happy to at least be able to determine correct size to use for upper case, no accents or special characters fonts (it would be a start, though naturally I would need to be able to use any characters).<br>
  But at least some way to determine the size and <strong>position</strong> (relative to the (0,0) of item) of the text content even in the simplest case.....</p>
  
  <p>The font metrics <code>tightBoundingRect()</code> seems the most accurate for size, but it seems impossible to determine its position so that I can somehow create my items correctly, and maybe resize/shift them correctly to fit on canvas.</p>
  
  <hr>
  
  <p>Here are some examples of my struggle to determine at least exact size and position of text, relative to the (0,0) of the item (assuming that once I do that, I am able to expose that info to outside or include the shift in the item transform on resize).<br>
  Notice that the size of the text advertised by font metrics does not always cover the text, and for different fonts I am not able to position the tight bounding rect (magenta) around the text itself. (I did multiple guesses, the code below is just one - the lines are trying to show different font metrics sizes).</p>
  
  <p><a href="https://i.stack.imgur.com/rCvmS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rCvmS.png" alt="enter image description here"></a></p>
  
  <p>The above were experiments in paint function of the text item inheriting <code>QGraphicsTextItem</code>:</p>
  
  <pre><code>void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
  {
      // draw text
      QGraphicsTextItem::paint(painter, option, widget);
  
      QPen p;
      p.setWidthF(0);
      QFontMetricsF fm(this-&gt;font());
      qreal ascent = fm.ascent(),
            descent = fm.descent(),
            hheight = fm.height();
      QRectF r = QGraphicsTextItem::boundingRect();
      QRectF rFont= fm.tightBoundingRect(toPlainText());
  
      qreal xmax = r.right();
  
      painter-&gt;save();
      painter-&gt;setBrush(Qt::NoBrush);
  
      // where is "ascent + descent"
      p.setColor(Qt::green);
      painter-&gt;setPen(p);
      painter-&gt;drawLine(QPointF(2, ascent), QPointF(2, ascent + descent));
      painter-&gt;drawLine(QPointF(2, ascent + descent), QPointF(xmax/2, ascent + descent));
  
      // where is "height"
      p.setColor(Qt::red);
      painter-&gt;setPen(p);
      painter-&gt;drawLine(QPointF(xmax/2, 0), QPointF(xmax/2, hheight));
      painter-&gt;drawLine(QPointF(xmax/2, ascent + descent), QPointF(xmax, ascent + descent));
  
      // where is "ascent"
      p.setColor(Qt::yellow);
      painter-&gt;setPen(p);
      painter-&gt;drawLine(QPointF(6, 0), QPointF(6, ascent));
      painter-&gt;drawLine(QPointF(6, ascent), QPointF(xmax, ascent));
  
      // something that may look like top of the text
      p.setColor(Qt::blue);
      painter-&gt;setPen(p);
      qreal yyy = ascent + rFont.y() + 1;
      painter-&gt;drawLine(QPointF(5, yyy), QPointF(xmax, yyy));
  
      // this should be useful... should be the natural offset
      qreal yoffset = (r.height() - rFont.height()) / 2;
  //    qDebug() &lt;&lt; yoffset &lt;&lt; r &lt;&lt; rFont;
      //qreal y0 = (r.height() - fm.height())/2;
  
      p.setColor(Qt::darkGreen);
      painter-&gt;drawEllipse(10, yoffset, 1, 1);
  
      // where is the font rect
      p.setColor(Qt::magenta);
      painter-&gt;setPen(p);
      yoffset = (r.height() + rFont.height()) / 2;
      painter-&gt;translate(0, yoffset);
      painter-&gt;drawRect(rFont);
      painter-&gt;restore();
  }
  </code></pre>
  
  <p>I have also tried not using <code>QGraphicsTextItem</code>, but paint text inside a rectangle. The same thing happens.</p>
  
  <p>(Qt 4.7 - 5.x)</p>