Results 1 to 11 of 11

Thread: Zoom effect by mouse Wheel in QGraphicsview

  1. #1
    Join Date
    Dec 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Zoom effect by mouse Wheel in QGraphicsview

    Dear Frds,
    In my project i have taken Qgraphicsscene and Qgraphicsview . I want to zoom the graphicsview using mouse wheel from the mouse pointer . I have tried but it not works with me . if i am zooming the view is not zooming from the mouse poiinter...............


    anybody can help me ..............

  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: Zoom effect by mouse Wheel in QGraphicsview

    What did you try, show us somthing.

    You need to get the scroll steps fomr the wheel event and apply the appripriate transformation on the view.
    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. #3
    Join Date
    Dec 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zoom effect by mouse Wheel in QGraphicsview

    Quote Originally Posted by Santosh Reddy View Post
    What did you try, show us somthing.

    You need to get the scroll steps fomr the wheel event and apply the appripriate transformation on the view.
    void Widget::wheelEvent(QWheelEvent* event) {

    QPointF pointBeforeScale(ui->graphicsView->mapToScene(event->pos()));

    //Get the original screen centerpoint
    QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());
    ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnde rMouse);
    //Scale the view ie. do the zoom
    double scaleFactor = 1.15; //How fast we zoom
    if(event->delta() > 0) {
    //Zoom in
    ui->graphicsView->scale(scaleFactor, scaleFactor);
    } else {
    //Zooming out
    ui->graphicsView->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    }
    ui->graphicsView->setTransformationAnchor(QGraphicsView::NoAnchor );
    //Get the position after scaling, in scene coords
    QPointF pointAfterScale(ui->graphicsView->mapToScene(event->pos()));

    //Get the offset of how the screen moved
    QPointF offset = pointBeforeScale - pointAfterScale;

    //Adjust to the new center for correct zooming
    QPointF newCenter = screenCenter + offset;
    SetCenter(newCenter);

  4. #4
    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: Zoom effect by mouse Wheel in QGraphicsview

    very hard to read, please add [Code] tags


    Added after 10 minutes:


    Looks like you are not geting the calculations correct, check the following

    Qt Code:
    1. void Widget::wheelEvent(QWheelEvent* event)
    2. {
    3. // Typical Calculations (Ref Qt Doc)
    4. const int degrees = event->delta() / 8;
    5. int steps = degrees / 15;
    6.  
    7. // Declare below as class member vars and set default values as below
    8. // qreal h11 = 1.0
    9. // qreal h12 = 0
    10. // qreal h21 = 1.0
    11. // qreal h22 = 0
    12.  
    13. double scaleFactor = 1.0; //How fast we zoom
    14. const qreal minFactor = 1.0;
    15. const qreal maxFactor = 10.0;
    16. if(steps > 0)
    17. {
    18. h11 = (h11 >= maxFactor) ? h11 : (h11 + scaleFactor);
    19. h22 = (h22 >= maxFactor) ? h22 : (h22 + scaleFactor);
    20. }
    21. else
    22. {
    23. h11 = (h11 <= minFactor) ? minFactor : (h11 - scaleFactor);
    24. h22 = (h22 <= minFactor) ? minFactor : (h22 - scaleFactor);
    25. }
    26.  
    27. ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    28. ui->graphicsView->setTransform(QTransform(h11, h12, h21, h22, 0, 0));
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Santosh Reddy; 31st December 2012 at 13:37.
    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.

  5. #5
    Join Date
    Dec 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zoom effect by mouse Wheel in QGraphicsview

    Thank you for reply
    In my project i have Qgraphicview and also Qgraphicscene and i have added Qpainterpath in Qgraphicscene and Qgraphicscene is added in Qgraphicsview so i am able to do zoom in graphicsview but the zoom is not done from the mouse pointer but from the center of graphicsview.

    And another problem is that while drawing in the graphicsview, the view also move with drawing.
    so please help me ............

    ok dear i have slove the problem of moving of graphicview while drawing . i have set scenerect of graphicsview so the problem is slove but now while zooming is not done on the mouse pointer ........
    Last edited by chirudi; 1st January 2013 at 06:01.

  6. #6
    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: Zoom effect by mouse Wheel in QGraphicsview

    Did you try this
    Qt Code:
    1. ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    To copy to clipboard, switch view to plain text mode 
    Note that the effect of this property is noticeable when only a part of the scene is visible (i.e., when there are scroll bars). Otherwise, if the whole scene fits in the view, QGraphicsScene uses the view alignment to position the scene in the view.
    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. #7
    Join Date
    Dec 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zoom effect by mouse Wheel in QGraphicsview

    That i have tried but then also i am not getting the perfect zoom is there any problem of scenerect i have widget of size 1280*1024. and Graphicsview of size 1141*991 or suggest other tools on which i can draw my object on paintevent and also perform zoom effect on it .........

    thank's

  8. #8
    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: Zoom effect by mouse Wheel in QGraphicsview

    having the scene rect bigger thatn view rect shuold make the Zoom effect work as expected, is there any thing else you are missing.

    Other Tools: You can use a normal QWidget and implement paintevent, but you will need to handle zoom effect while painting (will not be as stright forward as in case of QGraphicsView).
    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.

  9. #9
    Join Date
    Dec 2012
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Zoom effect by mouse Wheel in QGraphicsview

    i have all done . but the zoom from mouse pointer is not done, zoom is doing to qgraphics view only

  10. #10
    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: Zoom effect by mouse Wheel in QGraphicsview

    See if this workaround works, add a QGraphicsRectItem, and set the QGraphicsRectItem rect larger than the screen (monitor) size
    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.

  11. #11
    Join Date
    Jun 2014
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Zoom effect by mouse Wheel in QGraphicsview

    you can find the solution in this form

    http://www.qtcentre.org/wiki/index.p...ng_and_Zooming


    I used it in my code like this :


    Qt Code:
    1. //in header file
    2.  
    3. protected:
    4. virtual void wheelEvent(QWheelEvent* event);
    5.  
    6. //in cpp file
    7. void Dialog::wheelEvent(QWheelEvent *event){
    8.  
    9. ui->graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    10. // Scale the view / do the zoom
    11. double scaleFactor = 1.15;
    12. if(event->delta() > 0) {
    13. // Zoom in
    14. ui->graphicsView-> scale(scaleFactor, scaleFactor);
    15.  
    16. } else {
    17. // Zooming out
    18. ui->graphicsView->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    19. }
    20.  
    21.  
    22. //ui->graphicsView->setTransform(QTransform(h11, h12, h21, h22, 0, 0));
    23. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Can usb mouse wheel work in Qt/E ?
    By earth in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 26th August 2011, 12:00
  2. Replies: 1
    Last Post: 3rd August 2011, 02:14
  3. Scrolling on Mouse Wheel
    By penny in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2011, 08:30
  4. QGraphicsView - zoom out using the right mouse button
    By dbrmik in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2009, 23:17
  5. QwtPlotMagnifier Invert Wheel Zoom
    By jmsbc in forum Qwt
    Replies: 2
    Last Post: 13th March 2009, 00:12

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.