Results 1 to 2 of 2

Thread: Scaling a portion of an image to zoom into in QGraphicsView

  1. #1
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Scaling a portion of an image to zoom into in QGraphicsView

    Hello,

    I am using the Graphics framework to show images on the screen with a view, scene and items. I implemented a rubberband as well to "draw" rectangles on top of those images for the purpose of selecting a piece of the image to zoom into.

    So what I want is that when I make a selection of a subarea of my image currently fully displayed in my view, (clicking and holding the mouse, dragging and releasing to make the rubberband rectangle), I want to be able to now see only the selected area in my whole view, so basically have it zoom in to the portion I selected and fit in my viewport only the selected portion, and now of course the scroll bars will appear since the image doesn't fit in the view.

    I'm having a rough time "connecting" or mapping my selected rectangle in the image to calculate how much to scale the image and to have the view display the correct area that I chose.
    Can someone please help me out on what steps do I need to do for this task?

    I was first thinking to get the coordinates of the rubberband rectangle through (Inside my mouseDoubleClickEvent):

    m_rubberband->pos().x() //but in fact what x coordinate does that give me? The topleft corner in this case?
    m_rubberband->pos().y()


    Somehow I need to calculate how much of the image I selected to be able to scale it accordingly? Then the problem of how to make the viewport display correctly what I selected is giving me a hard time..
    Any help will be greatly appreciated.

    Thank you!

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Scaling a portion of an image to zoom into in QGraphicsView

    Assuming that you have a rect you want to display (from mouse press/release events) then simplest thing to do is to use:
    Qt Code:
    1. QGraphicsView::fitInView ( const QRectF& rect, Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio )
    To copy to clipboard, switch view to plain text mode 
    You can use Qt::KeepAspectRatio if that's what you want.

    Also when getting top-left and bottom-right points for the rectangle you should map them to sceene coordinates:
    Qt Code:
    1. void ZoomableView::mousePressEvent( QMouseEvent* e )
    2. {
    3. selectedRect.setTopLeft(this->mapToScene(e->pos()));
    4. }
    5.  
    6. void ZoomableView::mouseReleaseEvent( QMouseEvent* e )
    7. {
    8. selectedRect.setBottomRight(this->mapToScene(e->pos()));
    9.  
    10. this->fitInView(selectedRect, Qt::KeepAspectRatio);
    11. }
    To copy to clipboard, switch view to plain text mode 
    You can also disable the scroll bars if you don't want them:
    Qt Code:
    1. QGraphicsView::setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    2. QGraphicsView::setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Spitfire for this useful post:

    Sergex (21st October 2011)

Similar Threads

  1. Can a portion of QGraphicsView be made to freeze?
    By penny in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2011, 07:43
  2. Show portion of an image
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2010, 08:10
  3. missing image with zoom (QGraphicsView)
    By avis_phoenix in forum Qt Programming
    Replies: 7
    Last Post: 18th March 2009, 13:09
  4. Displaying a portion of an image
    By tas in forum Qt Programming
    Replies: 4
    Last Post: 28th November 2008, 03:09
  5. widget qgraphicsview scaling
    By mistertoony in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2007, 22:46

Tags for this Thread

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.