PDA

View Full Version : QGraphicsView and QGraphicsScene geometry



meazza
6th May 2011, 15:20
Hello.

I need some tips from my fellow programmers. I am working with QGraphicsView and QGraphicsScene as the title implies and what I need some tips with is how to size/scale QGraphicsScene in proportion to the view.

What I have now is a view widget that expands as the window expands and fixed size to the scene. But what I want is the same behavior for the scene as a widget has when it expands with the mainwindow. And I am not sure how to do this in a effective way. I am thinking about scaling but do not know if there are better ways.

I have attached a picture to try and clarify what I want. If you look at the picture, I want the items in the scene have the same proportions when I maximize the window.

high_flyer
9th May 2011, 10:00
I am thinking about scaling but do not know if there are better ways.
yes, scaling is the way to go.

meazza
1st July 2011, 16:58
Reviving this thread again instead of starting a new one so hopefully someone is gonna answer. What I am trying to accomplish is what is stated in my first post here. When I change the size of my window I want the items to have the same proportions.

Trying to implement this I came across a problem. What I do is in QGraphicsView::resizeEvent() I re-position and resize all the items in the scene depending on the size of the QGraphicsView change in procent. And this works ok, not fully happy so if you have some other suggestion I would gladly listen.

But my main question here is possible to set some kind of size policy on a widget so it is always a square. In this situation I want my QGraphicsView to always have equal width and height. But I dont know if I like that idea. The problem here is that tachometer item that you see on the attached picture needs to stay as a square or it gets deformed and that is where I got the idea of keeping the QGraphicsView square.

Dont know if I am making any sense here, has been a long day and this will be the last thing I do today :)

d_stranz
1st July 2011, 19:15
In your resize event, you could try a call to


QGraphicsView::fitInView( scene().sceneRect(), Qt::KeepAspectRatio );

This will maintain the same relative sizes and aspect ratio of everything in the scene, *except* those items with the QGraphicsItem::ItemIgnoresTransformations flag set. So if your tach's rect is square in scene coordinates, it will be square in the view.

meazza
4th July 2011, 11:45
In your resize event, you could try a call to


QGraphicsView::fitInView( scene().sceneRect(), Qt::KeepAspectRatio );

This will maintain the same relative sizes and aspect ratio of everything in the scene, *except* those items with the QGraphicsItem::ItemIgnoresTransformations flag set. So if your tach's rect is square in scene coordinates, it will be square in the view.

Thank you very much, this was what I was looking for