如果我想在 QGraphicsView 中放置一个字母(QGraphicsTextItem),有什么方法可以将文本设置为显示在我指定的坐标中间?如果我使用 QGraphicsTextItem.setPos(x,y),它会将 (x,y) 视为左上角。不过,我仍然希望 QGraphicsScene 的左上角保持为 (0, 0)。
请您参考如下方法:
我相信您应该能够使用 boundingRect QGraphicsTextItem 的方法来计算字母的高度和宽度,然后移动 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);
希望这对你有帮助,问候
