gpt4 book ai didi

python - 在 PyQt 中设置 QGraphicsTextItem 来源

转载 作者:太空宇宙 更新时间:2023-11-04 06:26:54 30 4
gpt4 key购买 nike

如果我想在 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);

希望这对你有帮助,问候

关于python - 在 PyQt 中设置 QGraphicsTextItem 来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7341635/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com