【问题标题】:graphicsView scale ignoring in drawbackgroundgraphicsView 比例忽略在背景中
【发布时间】:2013-06-19 05:24:29
【问题描述】:

我刚刚在 QGraphicsView 的背景上绘制了网格线

  drawbackground(QPainter *painter, const QRectF &rect)
    {
    QVarLengthArray<QLineF, 100> linesX;
    for (qreal x = left; x < sceneRect.right(); x += gridInterval )
    {

    linesX.append(QLineF(x, sceneRect.top(), x, sceneRect.bottom()));
    }

    QVarLengthArray<QLineF, 100> linesY;
    for (qreal y = top; y < sceneRect.bottom(); y += gridInterval ){

    linesY.append(QLineF(sceneRect.left(), y, sceneRect.right(), y));
    }

    painter->drawLines(linesX.data(), linesX.size());
    painter->drawLines(linesY.data(), linesY.size());
    }

我通过

缩放视图
void
ViewPort::zoomIn()
{
    zoom(qreal(1.2));
}

void
ViewPort::zoomOut()
{
    zoom(1 / qreal(1.2));
}

void
ViewPort::zoom(qreal scaleFactor)
{

    qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();

    if (factor < 1 || factor > 100)
        return;

    scale(scaleFactor, scaleFactor);

}

现在场景中有很多我必须缩放的项目,但我需要忽略我在 graphicsView drawBackground 上绘制的网格线。 我怎么能忽略gridLines的比例变换?

我尝试了 QPainter::resetTransform() 但徒劳无功..

【问题讨论】:

  • 出于好奇,为什么不想让网格线随着视图的比例缩放(缩放)——当然,网格的整个点都是为了指示比例?
  • 我只是想缩放或放大场景中的对象,如圆形目标、多边形目标和其他像素图目标。所以在视图中,网格线更像是带有轴标记的图表。
  • 我必须根据场景转换更新标记中的比例值。例如:根据sceneRect,默认比例将为x:1000,y:1000。如果我将场景缩放到 1500、1000,那么我必须将轴更新为 x:1500、y:1000。

标签: qt qgraphicsview


【解决方案1】:

如果将线条聚合到从 QGraphicsItem 派生的对象中并将项目添加到场景中,则可以设置标志 QGraphicsItem::ItemIgnoresTransformations,以阻止它响应缩放和其他转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 2017-05-25
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多