You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QGraphicsWidget boundingRect仍在视口内却消失的问题求助

QGraphicsWidget平移后部分在视口内却消失的问题排查

最近遇到一个头疼的问题:我做了一个代表地图上线条的QGraphicsWidget,但发现当它的boundingRect底部小部分移出场景,或者右侧部分移出场景时,整个对象居然会消失!具体现象如下:

  • 绿色方框是该对象paint函数绘制的boundingRect,此时对象正常显示:
    Object is visible
  • 当我平移场景到仅显示对象的这部分时,它还在视口里,但只要再向右平移哪怕一个像素——哪怕boundingRect还有一半在视口内——对象直接就消失了:
    Object will disappear if panned one more pixel right.
  • 向下平移让对象底部移出视口时也会出现同样的情况,超出下图的平移量后对象就没了:
    enter image description here

先贴一下paint函数的代码:

void AirwayGraphicsWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { 
    QPen pen(m_aw_obj->AltitudeType() == stds::rdb::WPAltitudeTypes::HIGH ? COLOR_HI_AIRWAY : 
             m_aw_obj->AltitudeType() == stds::rdb::WPAltitudeTypes::LOW ? COLOR_LO_AIRWAY : COLOR_BOTH_AIRWAY); 
    pen.setWidth(3); 
    pen.setCosmetic(true); 
    painter->setPen(pen); 
    painter->drawPolyline(m_points.data(), m_points.size()); 

    pen.setColor(m_is_hovered ? Qt::red : Qt::green); 
    pen.setWidth(0); 
    painter->setPen(pen); 
    painter->drawRect(boundingRect()); 
}

后来补充一下构造器和boundingRect的代码:

AirwayGraphicsWidget::AirwayGraphicsWidget(const stds::rdb::model::EnrouteAirway* aw, QGraphicsItem* parent) 
    : RDBEntityGraphicsWidget(aw, parent) 
    , m_bounding_rect() 
    , m_aw_obj(aw) 
    , m_lod(LevelOfDetail::NONE) 
    , m_is_hovered(false) { 

    double highest_lat = std::numeric_limits<double>::lowest(); 
    double lowest_lat = std::numeric_limits<double>::max(); 
    double highest_lon = std::numeric_limits<double>::lowest(); 
    double lowest_lon = std::numeric_limits<double>::max(); 

    for (auto &awd : m_aw_obj->AirwayData()) { 
        if (-awd.entity->Pos().latitude_deg > highest_lat) { 
            highest_lat = -awd.entity->Pos().latitude_deg; 
        } 
        if (-awd.entity->Pos().latitude_deg < lowest_lat) { 
            lowest_lat = -awd.entity->Pos().latitude_deg; 
        } 
        if (awd.entity->Pos().longitude_deg > highest_lon) { 
            highest_lon = awd.entity->Pos().longitude_deg; 
        } 
        if (awd.entity->Pos().longitude_deg < lowest_lon) { 
            lowest_lon = awd.entity->Pos().longitude_deg; 
        } 
        m_points.push_back(QPointF(awd.entity->Pos().longitude_deg, -awd.entity->Pos().latitude_deg)); 
    } 

    highest_lat += 5.0 * geom::NM_TO_DEG; 
    lowest_lat -= 5.0 * geom::NM_TO_DEG; 
    highest_lon += 5.0 * geom::NM_TO_DEG; 
    lowest_lon -= 5.0 * geom::NM_TO_DEG; 

    prepareGeometryChange(); 
    m_bounding_rect = QRectF(QPointF(highest_lon, highest_lat), QPointF(lowest_lon, lowest_lat)); 
    setPreferredHeight(m_bounding_rect.height()); 
    setPreferredWidth(m_bounding_rect.width()); 
    setAcceptHoverEvents(true); 
} 

QRectF AirwayGraphicsWidget::boundingRect() const { 
    return m_bounding_rect; 
}

我已经对着代码调试好几天了,各种检查还是没找到问题根源,真心恳请各位大佬帮忙指点一下,到底哪里出问题了?

内容的提问来源于stack exchange,提问作者jasonlg3d

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:01:13