开发者

Set QGraphicsTextItem origin in PyQt

开发者 https://www.devze.com 2023-04-03 15:09 出处:网络
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

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,开发者_JAVA百科 0), though.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号