【问题标题】:How to move a QGraphicsItem when scrolling the QGraphicsView?滚动 QGraphicsView 时如何移动 QGraphicsItem?
【发布时间】:2013-07-09 18:46:16
【问题描述】:

我有一个 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 类型。请帮忙,提前谢谢。

【问题讨论】:

    标签: qt


    【解决方案1】:

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

    // 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);
    } 
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 带有此标志的项目将忽略缩放和旋转,但不会忽略视图的滚动。视图的滚动不会作为真正的变换应用于场景。
      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多