You are not logged in.


Saturday, November 5th 2011, 4:46pm

view independent scaling of QGraphicsItems


I want to create a curve editor similar to the one from Blender - the animation curves editor. One of the core features is the ability to scale the view so that I can enlarge certain parts of the curve and edit it from up close.

I'm using a QGraphicsScene and a QGraphicsView for the curve editor. The curve itself is represented by a custom QGraphicsWidget that keeps a list of points and draws a polyline.

But when it comes to scaling, it doesn't work quite as expected. When I call the QGraphicsView::scale( qreal, qreal ) method, the view gets enlarged as if in a magnifying glass ( the lines get wider etc ).

I overcame it by querying the QPainter::deviceTransform() method and rescaling the width of the pens I'm drawing my lines with, but another problem remains - even if I enlrage the view quite considerably ( so that I even loose track of my curve, 'cause it's so huge ), no scroll bars appear - as if the bounding rectangles of the view were not refreshed.

Could someone please help me understand how to get it right?

Thanks :)


Sunday, November 6th 2011, 10:39am

if you think it is a bounding rect problem, then I suggest you output some debug info about your bounding rects... If you have a custom widget, I think you may need to do your own bounding rect method?…ml#boundingRect
Although the item's shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items' transformation.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.


Sunday, November 6th 2011, 5:17pm

It sounds like you want to change the data you're displaying in the graph, not scale the image of the graph?

I think I would approach that a different way.
I would not scale the view but instead manually keep track of where the user is and how much of the data is visible.
That's much more involved though.
Perhaps Qt already has something similar? Unfortunately I'm not aware of anything like that.


Friday, November 11th 2011, 11:00am

Thanks for responses.

I studied the problem in-depth, and there's more to it - basically I managed to get the scaled view to display the scroll bars after all - after each call to QGraphicsView::scale method I added the following call:

Source code

QRectF rect = scene()->sceneRect();setSceneRect( rect );

Worked like a charm...

Or did it - after a while I ran into a new problem: I'm adding two QGraphicsItems to the scene displayed by this view - two very long lines, one horizontal and one vertical.

The problem appears when the view is enlarged and I'm scrolling it horizontally - there's a magic point behind which the vertical line disappears. This point shifts right when I increase the scale, which would suggest that the scene tests the item's geometry against an unscaled bounding box or something like that.
But that's not the weird part - this issue occurs only if I add the horizontal lineto the scene first and the vertical one second. If I add the vertical one first, everything works fine.

Where's the sense in that?

EDIT - I debugged their code - it all boils down to a problem in the BSP tree they are using in the QGraphicsScene :(


This post has been edited 1 times, last edit by "paksas" (Nov 11th 2011, 11:12am)