PDA

View Full Version : QGraphicsView keep aspect ratio of items when scaled



^NyAw^
29th January 2013, 09:36
Hi,

I'm trying to use QGraphicsView and QGraphicsScene to do represent a plastic film that is 0.3meter width and 100meter height. In this plastic film there are some marks that I want to show to the user. The problem is that the aspect ratio of the film. Using the "fitInView" method keeping the aspect ratio gets me a view like you can see on the attached image.
8644
The black line is the rectangle that represents the full scene.

Without keeping the aspect ratio I get what I want but with the problem that the items also don't keep their aspect ratio, so a circle is displayed as an ellipse.

What I need is to show the full scene into the view(sure that will not keep the aspect ratio) but show the items with the original aspect ratio.

Thanks,

P.D.: I don't want the scroll bars to be used. This is why I want to show the full scene fitting the view.

Maybe the use of Qwt will be simple as I only want to let the user to pick the marks to show more information.

wysota
29th January 2013, 13:31
One thing is that you can always hide scrollbars if you don't want them. Another thing is that you can pass KeepAspectRatioByExpanding to fitInView.

^NyAw^
29th January 2013, 15:46
Hi,

What i need is to full fit the scene into the view but also need to display the items keeping the original aspect ratio. This is, when I add a square rectangle I want it to be displayed square or if I add a circle I want it to be showed as a circle, not an ellipse.

Passing KeepAspectRatioByExpanding the scene will get out of the view limits(as I understand).

Thanks,

wysota
29th January 2013, 16:08
I think your requirements are contradictory. You want to display something that doesn't fit your display in a way that it fits your display but without reducing the size in the other dimension at the same time keeping the aspect ratio. This is simply not possible. Either the larger dimension will not fit into the view, the smaller dimension will have to be scaled down or the aspect ration will have to be ignored.

^NyAw^
29th January 2013, 17:16
Hi,

Yes, it's a little bit contradictory. Maybe I'm trying to use QGraphicsView because I need that the items can be selected.

What I need is to represent the plastic film on a Widget. Then I need to represent every mark as a circle on this Widget. Finally the marks need to be selectable.

As you can see on the attached image, the gray rectangle represents the full film and the circles represent marks on the film.
8648

Maybe there is a better way to do this but I don't know wich.

Thanks,

Added after 56 minutes:

Hi,

Finally I changed the way of doing this.
First, the GraphicsScene will be resized to the GraphicsView size. Then I will map the real world (x,y) positions to scene positions taking account the scene and real world sizes.

Thanks,

wysota
29th January 2013, 19:31
I still fail to see how this lets you fit 100m long film on a ~1000px display while keeping aspect ratio.

^NyAw^
30th January 2013, 08:57
Hi,



I still fail to see how this lets you fit 100m long film on a ~1000px display while keeping aspect ratio.


No, what I want is the items that represent the marks to be circular. The film can be represented without keeping the aspect ratio.
Now I change the scene rect to use the same size as the view and I calculate the X and Y resolutions to map the values to the scene. This let me add circles to represent the marks.
The only thing that I need now is to get a signal when one item is selected ... Looking the documentation

lanz
30th January 2013, 12:31
You can try using QGraphicsItem::ItemIgnoresTransformations flag on your circles. That way you don't need to recalculate coordinates by hand.
You can reimplement mouse events for your items to get mouse messages.

^NyAw^
30th January 2013, 14:56
Hi,

I had done as I explained and it works as expected. Maybe your solution will also work.

Thanks,

d_stranz
30th January 2013, 15:42
The only thing that I need now is to get a signal when one item is selected ... Looking the documentation

Look at the part that describes the QGraphicsItem flags, especially the one about ItemIsSelectable...

There is an alternative way to do what you want to do: Instead of going through all kinds of complicated code to scale your image to fit the screen coordinates, just apply a scale QTransform to the ellipses to make them circular. That way, you can keep the scene in units of your image, but simply invert the transformation that maps scene coordinates to view coordinates when your circles are recalculated after zooming.

In either case, this means that your scene is locked to use in a single view, because you are recalculating coordinates based on the view, and not letting the G/V architecture take care of it automatically for you. So when your users ask you for a display where you show the entire film with a small rectangle over the zoomed in area next to the zoomed image, you'll need two scenes, thereby duplicating everything.

wysota
31st January 2013, 09:56
Circles can always be drawn outside the graphics view architecture.

^NyAw^
31st January 2013, 12:05
Hi



Circles can always be drawn outside the graphics view architecture.


Yes I know that I can draw them without using graphicsView but I use it to get the item selected signal without having to write too much code.
It also can be done using one widget representing the film and a set of widgets over the film widget representing the marks and giving me the clicked signal.
Don't worry much more for this, it's working now and for the moment it's a good solution for me.

Thanks,

wysota
31st January 2013, 21:26
Yes I know that I can draw them without using graphicsView but I use it to get the item selected signal without having to write too much code.
Seeing the problems you had so far I would risk saying it would be fewer lines of code than you have already written :)


It also can be done using one widget representing the film and a set of widgets over the film widget representing the marks and giving me the clicked signal.
That would rather be overcompication of things.

^NyAw^
1st February 2013, 08:37
Hi,



Seeing the problems you had so far I would risk saying it would be fewer lines of code than you have already written


Yes, but think on that learning is not a waste of time. :)