QGraphicsView and QGraphicsItem: don´t scale item when scaling the view rect

后端 未结 5 1629
既然无缘
既然无缘 2021-02-04 13:46

I am using Qt´s QGraphicsView - and QGraphicsItem-subclasses. is there a way to not scale the graphical representation of the item in the view when the

5条回答
  •  醉话见心
    2021-02-04 14:15

    The following solution worked perfectly for me:

    void MyDerivedQGraphicsItem::paint(QPainter *painter, const StyleOptionGraphicsItem *option, QWidget *widget)
    {
        double scaleValue = scale()/painter->transform().m11();
        painter->save();
        painter->scale(scaleValue, scaleValue);
        painter->drawText(...);
        painter->restore();
        ...
    }
    

    We can also multiply the scaleValue by other mesures we want to keep its size constant outside the save/restore environment.

        QPointF ref(500, 500);
        QPointF vector = scaleValue * QPointF(100, 100);
        painter->drawLine(ref+vector, ref-vector);
    

提交回复
热议问题