代码之家  ›  专栏  ›  技术社区  ›  Luper Rouch François Lagunas

获取qt图形视图中常量大小项的边界框

  •  2
  • Luper Rouch François Lagunas  · 技术社区  · 15 年前

    我需要得到(场景空间中)的边界框 QGraphicsItem 拥有的 QGraphicsItem::ItemIgnoresTransformations 标志集。

    根据文件,你需要使用 QGraphicsItem::deviceTransform() 这样做。我试过这个:

    // Get the viewport => scene transform
    vp_trans = view.viewportTransform();
    // Get the item => viewport transform
    trans = item.deviceTransform(vp_trans);
    // Get the item's bounding box in item's space
    bbox = item.boundingRect();
    // Map it to viewport space
    bbox = trans.mapRect(bbox);
    // Map it back to scene's space
    bbox = vp_trans.mapRect(bbox);
    

    但是有点问题,边界框看起来更小,而且在项目的右边很远…

    1 回复  |  直到 15 年前
        1
  •  3
  •   Luper Rouch François Lagunas    15 年前

    刚刚解决了,qgraphicsView::viewPortTransform()文档说“ 返回将视区坐标映射到场景坐标的矩阵 “,但实际上它返回 场景 视口 转换。

    在最后一步中反转vp_trans解决了我的问题。