2

如果我想在 QGraphicsView 中放置一个字母(一个 QGraphicsTextItem),有什么方法可以将文本设置为在我指定的坐标中间显示?如果我使用QGraphicsTextItem.setPos(x,y),它将 (x,y) 视为左上角。不过,我仍然希望 QGraphicsScene 的左上角保持为 (0, 0)。

4

1 回答 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);  

希望这会有所帮助,问候

于 2011-09-08T02:24:10.140 回答