Hello,

I'm trying to draw a rect around a text. I have the problem, that the rect is much too small in comparison to my text. I'm trying to get the length of the text with QFontMetrics. Here is a small example:

Qt Code:
  1. QPainter p(this);
  2.  
  3. QFont font;
  4. font.setFamily("Arial");
  5. font.setPixelSize(10);
  6. font.setBold(false);
  7.  
  8. QFontMetricsF metrics(font);
  9.  
  10. qreal width = metrics.width("Test");
  11. p.drawRect(0,0,width, 20);
  12. p.drawText(QRect(0,0,width,20),"Test");
  13.  
  14. qreal width1 = metrics.width("LongTest");
  15. p.drawRect(0,20,width1, 20);
  16. p.drawText(QRect(0,20,width1,20),"LongTest");
  17.  
  18. qreal width2 = metrics.width("LongLongTest");
  19. p.drawRect(0,40,width2, 20);
  20. p.drawText(QRect(0,40,width2,20),"LongLongTest");
To copy to clipboard, switch view to plain text mode 

The result in a QWidget you can see here:
Bildschirmfoto 2014-01-06 um 16.23.44.png

I'm using Qt 5 in a Kubuntu 13.04 system. Do I do anything wrong or is it a (huge) bug in Qt? I tested it with different font families and sizes. The result is always the same.

I hope you can help me!

Greetz
Screamer