1

我有一个 QGraphicsScene 和一个显示它的 QGraphicsView。我需要在场景中添加一个 QGraphicsItem 并将其保持在相同的位置,即使我滚动视图也是如此。我尝试如下覆盖视图的 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 类型。请帮助,提前谢谢。

4

2 回答 2

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);
} 
于 2013-07-09T10:46:16.783 回答
1

我认为更简单的方法实际上与您所要求的相反:尝试将标志设置ItemIgnoresTransformationsQGraphicsItem::GraphicsItemFlags.

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

于 2013-07-09T10:05:48.493 回答