Results 1 to 5 of 5

Thread: Questions about zooming QGraphicsView

  1. #1
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Questions about zooming QGraphicsView

    I've created a form with Qt Creator, on which I have a QGraphicsView object.
    I'd like to be able to zoom this item by intercepting the the wheelEvent function, but I don't want to have to subclass QGraphicsView and override wheelEvent there, so instead I overrode wheelEvent in the parent frame:

    Qt Code:
    1. void MyFrame::wheelEvent(QWheelEvent* event)
    2. {
    3. if (_view->underMouse())
    4. {
    5. if (event->delta() > 0)
    6. _view->scale(2, 2);
    7. else if (event->delta() < 0)
    8. _view->scale(0.5, 0.5);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Where _view refers to my QGraphicsView object. This seems a bit clunky but it appears to work. Is there a better way of doing this?


    Now the second question: although the above works, I notice that as soon as scroll bars appear in the QGraphicsView, then this functionality fails, and scrolling the mousewheel will instead cause the view to scroll. I presume this is because wheelEvent is being intercepted somewhere else, before my frame can handle it. Is there a way around this? I want the mousewheel to ALWAYS scroll the view.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Questions about zooming QGraphicsView

    Quote Originally Posted by JovianGhost View Post
    but I don't want to have to subclass QGraphicsView and override wheelEvent there
    Why not?

    Where _view refers to my QGraphicsView object. This seems a bit clunky but it appears to work. Is there a better way of doing this?
    Yes, subclass your view!

    NI presume this is because wheelEvent is being intercepted somewhere else
    Yes, by your view. That wouldn't be a problem if you would subclass your view. But as a workaround you can install an event filter on your view and handle the weel event there, or better do not handle it there so your function comes to play again.

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

    JovianGhost (20th March 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    55
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Questions about zooming QGraphicsView

    Quote Originally Posted by Lykurg View Post
    Why not?
    Well, I suppose the main reason is that I want to keep all GUI-related design in the GUI editor. If I subclass QGraphicsView, then I have to manually code instantiation and adding it where I want it. I know it doesn't sound like a big deal, and actually sounds pretty silly, but the way I see it part of the benfit of having a WYSIWYG GUI designer is that you don't have to worry about this stuff anymore. Guess I'm just spoiled.

    Yes, by your view. That wouldn't be a problem if you would subclass your view. But as a workaround you can install an event filter on your view and handle the weel event there, or better do not handle it there so your function comes to play again.
    Well, I guess that's as good of an argument as any to subclass QGraphicsView.
    If only I had a nickel for every time I had to subclass an existing class to make one minor change...

    PS It worked, thanks!
    Last edited by JovianGhost; 20th March 2010 at 20:42.

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Questions about zooming QGraphicsView

    Quote Originally Posted by JovianGhost View Post
    Well, I suppose the main reason is that I want to keep all GUI-related design in the GUI editor. If I subclass QGraphicsView, then I have to manually code instantiation and adding it where I want it. I know it doesn't sound like a big deal, and actually sounds pretty silly, but the way I see it part of the benfit of having a WYSIWYG GUI designer is that you don't have to worry about this stuff anymore. Guess I'm just spoiled.
    First, I am fine that you don't want to subclass!
    But I always wonder why people don't want to do so. As for your WYSIWYG argument: You can promote a widget in designer. So you can use your subclass and in designer it behaves like a normal graphics view and sets all properties correctly to your prometed subclass.

  6. #5
    Join Date
    Aug 2008
    Location
    Texas, USA
    Posts
    44
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Questions about zooming QGraphicsView

    Quote Originally Posted by Lykurg View Post

    But as a workaround you can install an event filter on your view and handle the weel event there, or better do not handle it there so your function comes to play again.
    I've tried going with the event filter route. It still seems to only emit the Wheel event when the scroll bar is at one of its extents.

    *EDIT* I take that back. If I use the eventFilter from a QGraphicsView I only get the mouse wheel event when the scrollbar is at one end. However if I install and eventFilter for the QGraphicsScene I get the mouse wheel event when it is not scrolled at the top or bottom as a QGrahpicsWheel event. So I need to install an event filter for both?
    Last edited by GimpMaster; 30th March 2010 at 15:12.

Similar Threads

  1. Replies: 0
    Last Post: 10th October 2009, 05:44
  2. zooming only x
    By mastupristi in forum Qwt
    Replies: 1
    Last Post: 7th May 2009, 08:23
  3. Zooming is too slow with QGraphicsView
    By learning_qt in forum Qt Programming
    Replies: 10
    Last Post: 4th December 2008, 09:23
  4. QGraphicsView general questions
    By sincnarf in forum Qt Programming
    Replies: 5
    Last Post: 15th October 2007, 23:22
  5. Zooming in QGraphicsView
    By JonathanForQT4 in forum Newbie
    Replies: 3
    Last Post: 17th April 2007, 05:50

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.