Results 1 to 7 of 7

Thread: Qgraphicsview zoom issue

  1. #1
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Qgraphicsview zoom issue

    Iam using mouse wheel to zoom my view,I have a condition not to zoom out scene below original.
    But when i zoom out to original size I dont see original size ,some part is not visible in view port.

    When I debugged I found that if I zoom out from a zoomed scene ,just before it reaches scene's original size
    my factor becomes 0.9999 so my condition fails and it doesn't zoom out anymore.

    Can anybody point our for me if I am doing something wrong here.

    Qt Code:
    1. void MyView::wheelEvent(QWheelEvent* event)
    2. {
    3.  
    4. qreal delta=pow((double)2,(event->delta())/ 120.0);
    5. qreal factor = matrix().scale(delta, delta).mapRect(QRectF(0, 0, 1, 1)).width();
    6.  
    7. if (factor < 1.000 || factor > 100)
    8. return;
    9.  
    10. this->scale(delta,delta);
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qgraphicsview zoom issue

    Try this
    Qt Code:
    1. void MyView::wheelEvent(QWheelEvent* event)
    2. {
    3. static qreal factor = 1.0;
    4.  
    5. const qreal delta = event->delta() / 120.0;
    6.  
    7. if(delta < 0)
    8. factor--;
    9. else if(delta > 0)
    10. factor++;
    11.  
    12. factor = qBound(1.0, factor, 100.0);
    13.  
    14. this->setTransform(QTransform(factor, 0, 0, factor, 0, 0));
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 19th April 2013 at 11:44.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    tarunrajsingh (22nd April 2013)

  4. #3
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Qgraphicsview zoom issue

    works like a charm:-)

  5. #4
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Qgraphicsview zoom issue

    One more question when you set transform for zooming (0,0) remains at top left always and the zoomed scene goes away from origin...I even set the anchorundermouse flag but still getting the same behaviour.I want my scene to zoom proportionally from all sides and also I have constraint that scene size should be equal to view size initially.

  6. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qgraphicsview zoom issue

    To apply transformation wrt to centre use QGraphicsView::AnchorViewCenter as transformatiojn anchor.

    To fit the scene to the view initially use QGraphicsView::fitInView()

    Just an exmaple, which was I used some time earlier.
    Qt Code:
    1. class Zoomer : public QObject
    2. {
    3. public:
    4. explicit Zoomer(QGraphicsView * parent) : QObject(parent), mH(1.0) { }
    5.  
    6. protected:
    7. bool eventFilter(QObject * object, QEvent * event)
    8. {
    9. QGraphicsView * mView = qobject_cast<QGraphicsView *>(object);
    10.  
    11. if(mView != 0)
    12. if(event->type() == QEvent::Wheel)
    13. {
    14. const QWheelEvent * wheelEvent = static_cast<QWheelEvent *>(event);
    15.  
    16. if(qApp->keyboardModifiers() & Qt::ControlModifier)
    17. {
    18. const int steps = wheelEvent->delta() / 120;
    19. static const double scaleFactor = 1.0;
    20. static const qreal minFactor = 1.0;
    21. static const qreal maxFactor = 100.0;
    22.  
    23. if(steps > 0)
    24. mH += scaleFactor;
    25. else
    26. mH -= scaleFactor;
    27.  
    28. mH = qBound(minFactor, mH, maxFactor);
    29.  
    30. mView->setTransformationAnchor(mView->AnchorUnderMouse);
    31. mView->setTransform(QTransform(mH, 0.0, 0.0, mH, 0, 0));
    32.  
    33. return true;
    34. }
    35. }
    36. return parent()->eventFilter(object, event);
    37. }
    38.  
    39. private:
    40. qreal mH;
    41. };
    42.  
    43. int main(int argc, char* argv[])
    44. {
    45. QApplication app(argc, argv);
    46.  
    47.  
    48. // Add a rectangle and a text item
    49. (new QGraphicsTextItem("Scene", new QGraphicsRectItem(0, 0, 200, 100, 0, &scene), &scene))->setPos(10, 10);
    50.  
    51. view.setScene(&scene);
    52. view.installEventFilter(new Zoomer(&view));
    53. view.fitInView(scene.sceneRect(), Qt::KeepAspectRatioByExpanding);
    54. view.showMaximized();
    55.  
    56. return app.exec();
    57. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 23rd April 2013 at 10:55.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. The following user says thank you to Santosh Reddy for this useful post:

    tarunrajsingh (23rd April 2013)

  8. #6
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: Qgraphicsview zoom issue

    Thanks ,I also need zoomed Rect in scene coordinates,Is there a way to get it from WheelEvent delta?

  9. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qgraphicsview zoom issue

    take the sceneRect() and apply the transformation on the rectagle manually.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Zoom effect by mouse Wheel in QGraphicsview
    By chirudi in forum General Programming
    Replies: 10
    Last Post: 21st June 2014, 08:19
  2. Replies: 1
    Last Post: 9th October 2012, 12:32
  3. Replies: 1
    Last Post: 17th October 2011, 13:56
  4. QGraphicsView - zoom out using the right mouse button
    By dbrmik in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 22:17
  5. missing image with zoom (QGraphicsView)
    By avis_phoenix in forum Qt Programming
    Replies: 7
    Last Post: 18th March 2009, 13:09

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.