2013-07-09 63 views
1

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

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的。请提前帮助,谢谢。

回答

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); 
} 
+0

非常感谢。此解决方案有效。 – kasper360

1

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

还有其他标志可以帮助你,看文档。

+0

带此标志的项目将忽略缩放和旋转,但不会忽略视图的滚动。视图的滚动不作为真正的转换应用于场景。 –