【问题标题】:Set QGraphicsTextItem origin in PyQt在 PyQt 中设置 QGraphicsTextItem 原点
【发布时间】:2011-09-08 10:24:10
【问题描述】:

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

【问题讨论】:

    标签: python qt pyqt qgraphicsview qgraphicsscene


    【解决方案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);  
    

    希望这会有所帮助,问候

    【讨论】:

    • 水平方向偏移了大约 1 个像素,但这可能是字体的问题。谢谢。
    猜你喜欢
    • 2019-07-10
    • 2020-02-10
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2016-07-30
    相关资源
    最近更新 更多