Results 1 to 14 of 14

Thread: QGraphicsView keep aspect ratio of items when scaled

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QGraphicsView keep aspect ratio of items when scaled

    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.
    keepAspectRatio.PNG
    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.
    Last edited by ^NyAw^; 29th January 2013 at 10:19.
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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,
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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.
    img.PNG

    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,
    Last edited by ^NyAw^; 29th January 2013 at 17:16.
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    I still fail to see how this lets you fit 100m long film on a ~1000px display while keeping aspect ratio.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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
    Last edited by ^NyAw^; 30th January 2013 at 09:06.
    Òscar Llarch i Galán

  8. #8
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    Hi,

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

    Thanks,
    Òscar Llarch i Galán

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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.

  11. The following user says thank you to d_stranz for this useful post:

    ^NyAw^ (30th January 2013)

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    Circles can always be drawn outside the graphics view architecture.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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,
    Òscar Llarch i Galán

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    Quote Originally Posted by ^NyAw^ View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    ^NyAw^ (1st February 2013)

  16. #14
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView keep aspect ratio of items when scaled

    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.
    Òscar Llarch i Galán

Similar Threads

  1. Get aspect ratio from Phonon videoWidget
    By budda in forum Qt Programming
    Replies: 2
    Last Post: 24th October 2012, 08:46
  2. Printing and Aspect ratio
    By cpt4 in forum Qwt
    Replies: 9
    Last Post: 30th September 2011, 14:28
  3. keeping aspect ratio while resizing
    By franco.amato in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2009, 20:12
  4. Preserving aspect ratio of image
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2009, 13:39
  5. aspect ratio of top-level widgets
    By urbangipsy in forum Qt Programming
    Replies: 3
    Last Post: 16th January 2007, 21:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.