Search code examples
c++qtqgraphicsviewqgraphicsitemqgraphicsscene

Text position scaling


I'm now using QGraphicsView and QGraphicsScene for showing some charts. Depending on values of that charts (they are histograms) I change the scale. I also draw some text (they are derived from QGraphicsItem) for showing their values like this

enter image description here

But I don't scale texts like charts, so it brings to a problem. If I don't scale texts, so I get it's bounding rect's real coordinates. I want to get scale of y axis for drawing texts in right positions.

So my question how may I get the scale in QGraphicsItem or in QGraphicsScene.

Thank you in advance.


Solution

  • Well, there is a solution for that. In QGraphicsScene you can get transformation matrix like this

    QTransform matrix = views().at(0)->transform();
    

    views() actually returns a QList of QGraphicsViews. After getting matrix for getting scale (for example vertical) you can do this

    qreal verticalScale = matrix.m22();
    

    Difference for getting scale in QGrapcsItem is just

    QTransform matrix = scene()->views().at(0)->transform();
    

    And the rest is same.