title: Center text vertically when drawing with QPainter's drawText()
body: <p>My strategy when centering text on images is to get bounding rectangle for that text and divide width or height by two. I did the same in this case. This is example I have created:</p>
  
  <pre><code>void CanvasWidget::paintEvent(QPaintEvent*)
  {
      //Create image:
      QImage image(rect().width(), rect().height(), QImage::Format_RGB32);
      QPainter paint(&amp;image);
      // White background
      image.fill(QColor("#FFF"));
      // set some metrics, position and the text to draw
      QFontMetrics metrics = paint.fontMetrics();
      int yposition = 100;
      QString text = "Hello world.";
      // Draw gray line to easily see if centering worked
      paint.setPen(QPen(QColor("#666"), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
      paint.drawLine(0, yposition, image.width(), yposition);
      // Get rectangle
      QRect fontRect = metrics.boundingRect(text);
      // Black text
      paint.setPen(QPen(QColor("#000"), 1, Qt::SolidLine, Qt::FlatCap, Qt::RoundJoin));
      // Add half the height to position (note that Qt has [0,0] coordinates at the bottom of the image
      paint.drawText(4, yposition+round(((double)fontRect.height())/2.0), text);
  
  
      QPainter p(this);
      p.drawImage(rect(), image, image.rect());
      p.end();
  }
  </code></pre>
  
  <p>This is the result - the text is under line instead centered on the line:</p>
  
  <p><sub>Android:</sub><br>
  <img src="https://i.stack.imgur.com/FNKg7.png" alt="image description"><br>
  <sub>Windows:</sub><br>
  <img src="https://i.stack.imgur.com/Qegmr.png" alt="image description"></p>
  
  <p>I used lines to draw frame around the text based on metrics rectangle:</p>
  
  <p><img src="https://i.stack.imgur.com/2uYGL.png" alt="image description"></p>
  
  <p>Intended result was to center <strong>visible text</strong> exactly around the given point/line:</p>
  
  <p><img src="https://i.stack.imgur.com/3LvbY.png" alt="image description"></p>
  
  <p>To put you in perspective, this is the <em>actual</em> problem I am having:<br>
  <img src="https://i.stack.imgur.com/uK2gz.png" alt="image description"><br>
  The numbers should be in the middle of the lines, not so much below.</p>
  
  <p>The function I am using returns size including accents and other big characters that <strong>aren't there</strong>. How do I get the rectangle in pixels <strong>only for characters that are there</strong>? </p>