【问题标题】:Co-ordinates for rectItem wrt to other rectItem in qgraphicsScenerect Item wrt到qgraphicsScene中其他rect Item的坐标
【发布时间】:2018-05-14 10:55:32
【问题描述】:

在 qgraphicsScene 中,我们添加了 2 个 rectItem。

假设首先添加红色矩形,它的左上角坐标是 (x1, y1) wrt 到 qgraphicsscene。现在在场景中添加了第二个蓝色矩形,它与红色矩形重叠。

现在我怎样才能得到红色矩形的坐标到蓝色矩形的坐标系。

没有对 rectItems 进行父级,它只是在场景中添加了 2 个 rectItems。 也尝试了 mapRectFromScene 等,但没有得到结果。

【问题讨论】:

    标签: qt qgraphicsview qgraphicsscene qgraphicsitem qgraphicsrectitem


    【解决方案1】:

    您可以使用 QTransform 对象计算蓝色矩形坐标的红色矩形坐标,如下所示:

    QRect red, blue; // somewhere in your code...
    
    QTransform t;
    t.translate(blue.x(), blue.y()); // move the origin to blue's top left corner
    QRect redWRTBlue = t.mapRect(red); // get a copy of red wrt blue's position
    

    redWRTBlue.x() 和 redWRT.y() 是你想要的。

    【讨论】:

      【解决方案2】:

      我搞定了,

      x = (red.x - blue.x)

      y = (red.y - blue.y)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-22
        • 2014-09-12
        • 2011-08-15
        • 1970-01-01
        • 2022-01-14
        • 1970-01-01
        • 2011-05-14
        • 2010-09-29
        相关资源
        最近更新 更多