Results 1 to 5 of 5

Thread: WheelEvent on just 'GraphicsView' and not in whole window

  1. #1
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    5

    Default WheelEvent on just 'GraphicsView' and not in whole window

    Hello,

    I have a Main Window and within it I have a bunch of UI elements like tab browsers, graphview and so on. It has been created using Qt Creator and not by explicitily making class objects for each component and adding it to the Main Window.

    I want to add wheelEvent for zoom in and zoom out actions on the GraphView section. But it effects the whole window. That is, I want the zoom-in and zoom-out to happen only when the WheelEvent is done in the graph view. But at present, anywhere in the main window I do the scrolling, the zooming takes place.

    How do I restrict the WheelEvent only to the graphview ? One way I thought was to capture the mouse position somehow and check if it is within the GraphView and then proceed. Secondly, may be adding the graphview as a separate class and instantiating it in the Main Window, and deleting it from the Qt Creator ui form.

    Please suggest how to achieve this.

    Thank you.

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

    Default Re: WheelEvent on just 'GraphicsView' and not in whole window

    Either promote your graphics view to a custom class and implement the event in that class or install an event filter on the graphics view and intercept wheel events in your window class.
    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. The following user says thank you to wysota for this useful post:

    gaganbm (7th February 2013)

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

    Default Re: WheelEvent on just 'GraphicsView' and not in whole window

    How are you currently doing the zoom ?
    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. #4
    Join Date
    Nov 2012
    Posts
    11
    Thanks
    5

    Default Re: WheelEvent on just 'GraphicsView' and not in whole window

    I got this piece of code :

    Qt Code:
    1. void MainWindow::wheelEvent(QWheelEvent* event) {
    2.  
    3. //Get the position of the mouse before scaling, in scene coords
    4. QPointF pointBeforeScale(ui->graphWidget->mapToScene(event->pos()));
    5.  
    6. //Get the original screen centerpoint
    7. QPointF screenCenter = GetCenter(); //CurrentCenterPoint; //(visRect.center());
    8.  
    9. //Scale the view ie. do the zoom
    10. double scaleFactor = 1.15; //How fast we zoom
    11. if(event->delta() > 0) {
    12. //Zoom in
    13. ui->graphWidget->scale(scaleFactor, scaleFactor);
    14. } else {
    15. //Zooming out
    16. ui->graphWidget->scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    17. }
    18.  
    19. //Get the position after scaling, in scene coords
    20. QPointF pointAfterScale(ui->graphWidget->mapToScene(event->pos()));
    21.  
    22. //Get the offset of how the screen moved
    23. QPointF offset = pointBeforeScale - pointAfterScale;
    24.  
    25. //Adjust to the new center for correct zooming
    26. QPointF newCenter = screenCenter + offset;
    27. SetCenter(newCenter);
    28. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, the GraphicsView is ui->graphWidget . But it is a part of the mainwindow, and not built programattically. It is part of the ui form of the main window.

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

    Default Re: WheelEvent on just 'GraphicsView' and not in whole window

    Nice, now move this implementation to a event filter object, and install the event filter on 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.

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

    gaganbm (7th February 2013)

Similar Threads

  1. Replies: 1
    Last Post: 21st March 2012, 13:44
  2. GraphicsView / Scene window minimize maximize buttons
    By erikusa in forum Qt Programming
    Replies: 0
    Last Post: 16th December 2010, 00:47
  3. How to disable wheelEvent()
    By Markus in forum Qt Programming
    Replies: 3
    Last Post: 6th August 2010, 18:42
  4. override wheelEvent for QScrollBar
    By ChasW in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2007, 09:50
  5. How to disable wheelEvent for scrollbars
    By forrestfsu in forum Qt Programming
    Replies: 7
    Last Post: 11th October 2006, 19:05

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