Results 1 to 6 of 6

Thread: WheelEvent reimplementation of a class derived from QGraphicsView

  1. #1
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default WheelEvent reimplementation of a class derived from QGraphicsView

    Hello,

    I reimplemented the wheelEvent in my graphicsView class to be able to zoom in and zoom out smoothly in width of an image. At first the implementation was:

    Qt Code:
    1. float exp = (event->delta() / 240.0);
    2. float factor = powf(3/4, exp);
    3. scale(factor, 1.0);
    To copy to clipboard, switch view to plain text mode 

    This works great, except that I don't want to be able to zoom out too much that the image starts being smaller than the widget that contains it. I want the container widget rectangle to be the minimum width of the image.

    For this I tried using transforms the following way:
    In the wheel event I did this instead:
    (item is my GraphicsItem object)

    Qt Code:
    1. //just changed the line : scale(factor, 1.0) to:
    2. item->setScale(factor);
    To copy to clipboard, switch view to plain text mode 

    and in my class derived from QGraphicsItem I added to methods:

    Qt Code:
    1. void setScale(float factor);
    2. void updateTransform();
    3.  
    4. //In their implementations I did:
    5.  
    6. void mygraphicsItem::setScale(float factor)
    7. {
    8. m_factor = factor;
    9. updateTransform();
    10. }
    11.  
    12. void mygraphicsItem::updateTransform()
    13. {
    14. QTransform transform;
    15. transform.scale(m_factor, 1.0);
    16. setTransform(transform);
    17. }
    To copy to clipboard, switch view to plain text mode 

    Now the part to prevent and control the zooming is not implemented yet since I am jsut testing this, but shouldn't something like this do the same thing as before when I was scaling the view instead of the item?

    Am I remotely on the right track to accomplish what I want? How can this be done?

    Thanks!

    This last way does nothing like before,

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

    Default Re: WheelEvent reimplementation of a class derived from QGraphicsView

    Isn't setScale an already existing non-virtual method of QGraphicsItem?
    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. #3
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: WheelEvent reimplementation of a class derived from QGraphicsView

    Quote Originally Posted by wysota View Post
    Isn't setScale an already existing non-virtual method of QGraphicsItem?
    Ok yes, it is. So I changed it to set setItemScale. Its not my point to overload the already existent function.

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

    Default Re: WheelEvent reimplementation of a class derived from QGraphicsView

    It wouldn't work anyway, would it?
    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.


  5. #5
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: WheelEvent reimplementation of a class derived from QGraphicsView

    If it did I wouldn't be posting in this forum now would I? Why bother with the useless rhetorical questions..

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: WheelEvent reimplementation of a class derived from QGraphicsView

    I mean trying to override the method wouldn't work. Your method simply could never be called.

    I admit I have no idea what you are trying to do, it seems obvious to try it this way:

    Qt Code:
    1. void Cls::wheelEvent(...) {
    2. qreal sc = item->scale();
    3. if(wheel goes up) sc+= 0.1; // or whatever
    4. else if(wheel goes down) sc -= 0.1;
    5. item->setScale(qBound(0.1, sc, 2.0));
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. Can not cast into the derived class from QScrollBar
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2008, 10:23
  2. no frame on Qlabel derived class
    By tupu_83 in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2008, 16:01
  3. Use QWidget derived class in Dialog
    By qtneuling in forum Qt Tools
    Replies: 2
    Last Post: 17th May 2008, 23:29
  4. rtti() of derived class
    By quickNitin in forum Newbie
    Replies: 4
    Last Post: 8th October 2006, 14:20
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36

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.