Results 1 to 6 of 6

Thread: Zooming with rubberband

  1. #1
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Zooming with rubberband

    Hi,

    I want to zoom into my graphics view with the rubberband. I want also my view center to be the rubberband's shape center. In other words, I want fill my whole view with that rubberband shape(rectangle).

    I looked at the documentation and I saw that setting dragMode as RubberBandDrag could be adequate to perform the task above but, I couldn't be quite sure about scale function of QGraphicsView. Will it be better to implement my own rubberband? Any suggestions? For example what abour using fitInView()?


    Thanks
    Last edited by zgulser; 23rd February 2010 at 18:02.

  2. #2
    Join Date
    May 2009
    Posts
    21
    Thanks
    4
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zooming with rubberband

    Hi,

    QGraphicsView::RubberBandDrag is normally used to select items within the rubberband but you can use it to display a rubberband and then do the zooming yourself.

    You just have to set the drag mode to rubberband and install a event filter to the view's viewport to handle the click events:

    Qt Code:
    1. view->setDragMode(QGraphicsView::RubberBandDrag)
    2. view->viewport()->installEventFilter(viewEventHandler)
    To copy to clipboard, switch view to plain text mode 


    The view event handler than gets called and can do the zooming:

    Qt Code:
    1. bool ViewEventHandler::eventFilter(QObject *o, QEvent *event )
    2. {
    3. bool filterEvent = false;
    4.  
    5. switch(event->type()) {
    6.  
    7. case QEvent::MouseButtonPress:
    8. {
    9. QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
    10. rubberBandOrigin = mouseEvent->pos();
    11. rubberBandActive = true;
    12. break;
    13. }
    14.  
    15.  
    16. case QEvent::MouseButtonRelease:
    17. {
    18. if (rubberBandActive) {
    19. QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
    20. QPoint rubberBandEnd = mouseEvent->pos();
    21.  
    22. QGraphicsView * view = static_cast<QGraphicsView *>(o->parent());
    23. QRectF zoomRectInScene = QRectF(view->mapToScene(rubberBandOrigin),
    24. view->mapToScene(rubberBandEnd));
    25.  
    26. view->fitInView(zoomRectInScene, Qt::KeepAspectRatio);
    27. rubberBandActive = false;
    28. }
    29. break;
    30. }
    31. }
    32.  
    33. return filterEvent;
    34. }
    To copy to clipboard, switch view to plain text mode 


    Another possibility would be to use QRubberBand.

    Regards

    Lodorot

  3. The following 2 users say thank you to Lodorot for this useful post:

    akiross (4th April 2011), zgulser (23rd February 2010)

  4. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Zooming with rubberband

    Hi again,

    I have few trivial questions..

    1. Then setting drag mode is just to visualize it?

    2. What about the items inside the view? Does the view framework will automatically do all transformations for the items?

    3. Is it possible to get rid of item selection when rubberbanding?

    4. What about centering? Will it be OK after?

    Finally, you said the other possibility is using QRubberBand..hımm but I think it would be very similar to yours.

    Thanks again

  5. #4
    Join Date
    May 2009
    Posts
    21
    Thanks
    4
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Zooming with rubberband

    Hi,

    about your questions:

    1. Yes, in this example the drag mode is only set to visualize the rubberband.

    2. The view will do the scaling and translation of the items

    3. To get rid of item selection you could use QRubberBand. Then you have to do the resizing of the QRubberBand as well.

    4. I quote the fitInView documentation:

    "Scales the view matrix and scrolls the scroll bars to ensure that the scene rectangle rect fits inside the viewport. ... rect will be centered in the view if it does not fit tightly."

    I would say just try it out.

    Regards


    Lodorot

  6. #5
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Zooming with rubberband

    Thanks again for your interest mate

  7. #6
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Zooming with rubberband

    Hi again,

    There appeared a problematic case. When I do fitInView, there happens a miscoordination between the cursor position and the start position of rubberband shape. I'm reimplementing mouse events in my view, not in the scene since it doesn't exist as a seperate class. Maybe the reason is that?

    By the way, after I do fitInView, I observed that left corner point, which is my origin, is not 0,0 anymore..

    Thanks
    Last edited by zgulser; 24th February 2010 at 12:36.

Similar Threads

  1. Graphics view rubberband zooming
    By Veeru in forum Qt Programming
    Replies: 5
    Last Post: 25th February 2010, 11:14
  2. RubberBand Problem
    By zgulser in forum Qt Programming
    Replies: 10
    Last Post: 16th February 2009, 07:05
  3. Drawing a Rubberband
    By daviddoria in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 14:32
  4. QGV: ItemIgnoresTransformations and RubberBand
    By Vladimir in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2007, 14:38
  5. Rubberband zoom
    By sreedhar in forum Qt Programming
    Replies: 3
    Last Post: 12th September 2006, 11:38

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
  •  
Qt is a trademark of The Qt Company.