滚动QGraphicsView时如何移动QGraphicsItem?

时间:2013-07-09 09:59:46

标签: qt

我有一个QGraphicsScene和一个显示它的QGraphicsView。我需要在场景中添加一个QGraphicsItem,即使滚动视图也要将它保持在同一位置。我尝试覆盖view的scrollContentsBy()方法如下,但它没有做到这一点。

void FETimelineView::scrollContentsBy( int dx, int dy )
{
    QGraphicsView::scrollContentsBy(dx, dy);

    QRectF oRect = p_CatBar->rect();
    p_CatBar->setPos( oRect.x() + dx, oRect.y() + dy );
}

顺便说一句,FETimelineView是我的QGraphicsView,p_CatBar是QGraphicsItem类型。请提前帮助,谢谢。

2 个答案:

答案 0 :(得分:3)

不是按滚动量移动它,而是可以获得您希望它相对于视图的位置,然后根据该位置直接设置它。所以它会是这样的: -

// Assuming that the Graphics Item top left needs to be at 50,50 
// and has a width and height of 30,20

void FETimelineView::scrollContentsBy( int dx, int dy )
{
    QGraphicsView::scrollContentsBy(dx, dy);

    // get the item's view position in scene coordinates
    QRect scenePos = mapToScene(QRect(50, 50, 30, 20));
    p_CatBar->setPos(scenePos);
} 

答案 1 :(得分:1)

我认为更简单的方法实际上与您提出的要求相反:尝试在ItemIgnoresTransformations中设置标记QGraphicsItem::GraphicsItemFlags

还有其他标志可以帮助您,请参阅文档。