Question

If I want to place one letter (a QGraphicsTextItem) in a QGraphicsView, is there any way I can set the text to display in the middle of the coordinates I specify? If I use QGraphicsTextItem.setPos(x,y), it treats the (x,y) as the top-left corner. I still want the top-left of the QGraphicsScene to remain as (0, 0), though.

Was it helpful?

Solution

I believe you should be able to use the boundingRect method of your QGraphicsTextItem to calculate the height and width of your letter and then shift position of the QGraphicsTextItem to make it look like it's centered around x,y coordinates. Smth like this:

QGraphicsTextItem* textItem0 = new QGraphicsTextItem("Test Text Item", 0, scene);
int x = x - textItem0->boundingRect().width() / 2;
int y = y - textItem0->boundingRect().height() / 2;
textItem0->setPos(x, y);  

hope this helps, regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top