Qt - Using a QTransform (or similar), scale inner QRect to/from QGraphics

2.3k Views Asked by At

Some background - Say you have QGraphicsScene, and only one view, which is a 1-1 scale with the scene. You have a QRect A, which is represents an external view of the scene, with a pre-defined pixel size. You have a QRect A1 which is a smaller rect inside of A.

How do you translate A1 to the scene, such that it is scaled correctly (i.e. if it's 1/4 of rect A, it will occupy 1/4 of the scene), and then undo that transform to scale a rect created in the scene to fit in rect A correctly?

I can do all this brute force, but I'm wondering if there's a way using Qt's built in classes...

1

There are 1 best solutions below

1
On

After looking over some examples to try and find similar uses, I realized I'm totally missing the point - I can just set A/A1 directly to the scene, and scale the view (via the totally obvious but somehow completely overlooked until now QGraphicsView::fitInView(..)) to fit the rects inside. No rect transforms necessary. Total 'duh' moment. :)

I will need to transform mouse clicks and points in the view when interacting with it, but there is a whole nice set of mapTo* mapFrom* that will handle that nicely.

TL;DR - Use fitInView()