Here's a very basic piece of code which:
- Measures the size a piece of text would take.
- Draws the rectangle which corresponds to this size at coordinates (100, 25).
- Displays text at coordinates (100, 25).
auto str = "Hello, World!";
auto metrix = window->fontMetrics();
auto text = scene->addText(str);
text->setPos(100, 25);
text->setDefaultTextColor(Qt::white);
auto r = metrix.boundingRect(str);
int x, y, w, h;
r.getRect(&x, &y, &w, &h);
scene->addRect(100, 25, w, h, QPen(Qt::white));
The scene
in code is a QGraphicsScene
with no specific customizations, with the exception of a border set to zero.
I would expect the text to be exactly inside the rectangle. The text is however shifted by a few pixels to the left and a few more pixels to the bottom. Why?