在PyQt中设置QGraphicsTextItem起点
如果我想在一个QGraphicsView里放一个字母(也就是一个QGraphicsTextItem),有没有办法让我设置的坐标中显示的文字正好在中间?如果我使用QGraphicsTextItem.setPos(x,y)
,它会把(x,y)当作左上角的位置。不过,我还是希望QGraphicsScene的左上角保持在(0, 0)这个位置。
1 个回答
1
我觉得你可以使用你的 QGraphicsTextItem 的 boundingRect 方法来计算字母的高度和宽度,然后调整 QGraphicsTextItem 的位置,这样它看起来就像是围绕 x,y 坐标居中。大概是这个意思:
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);
希望这能帮到你,祝好