Results 1 to 6 of 6

Thread: How to zooming like in AutoCAD with QGraphicsView?

  1. #1
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question How to zooming like in AutoCAD with QGraphicsView?

    I want to change behaviour of QGraphicsView (in subclass) to enable zooming via overriding wheelEvent. I want to zoom like in AutoCAD, i.e. point in scene under mouse cursor same before and after zooming. Also when scene view resized, sceneRect remain same (but expanding if nessessary to keep original aspect ratio of scene).
    I tried several math equations all of them works far for normal especially when scroll bars appears.
    Please, give some code snippets and what else i need to override in subclass?

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to zooming like in AutoCAD with QGraphicsView?

    One of the QGraphicsView tutorials has wheel zooming, along with the code. I don't recall which one, but it works fine for my purposes.

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to zooming like in AutoCAD with QGraphicsView?

    header :

    Qt Code:
    1. class CmwGraphicsView : public QGraphicsView
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit CmwGraphicsView(QWidget *parent = 0);
    6.  
    7. protected:
    8. void wheelEvent(QWheelEvent *event);
    9. void scaleView(qreal scaleFactor);
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 


    CPP :

    Qt Code:
    1. CmwGraphicsView::CmwGraphicsView(QWidget *parent) :
    2. QGraphicsView(parent)
    3. {
    4.  
    5. }
    6.  
    7. void CmwGraphicsView::scaleView(qreal scaleFactor)
    8. {
    9. qreal factor = matrix().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
    10. if (factor < 0.07 || factor > 100)
    11. return;
    12.  
    13. scale(scaleFactor, scaleFactor);
    14. }
    15.  
    16. void CmwGraphicsView::wheelEvent(QWheelEvent *event)
    17. {
    18. scaleView(pow((double)2, -event->delta() / 240.0));
    19. }
    To copy to clipboard, switch view to plain text mode 

    Of course this just zooms from the center of the view. You still have to work out zooming with the mouse pointer as center.

    I would first translate to the mouse position, and then do the zooming.

    Best regards,
    Marc

  4. #4
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to zooming like in AutoCAD with QGraphicsView?

    for zooming under the mouse cursor, use the function setResizeAnchor. this function set the possible anchors that QGraphicsView can use when the user resizes the view or when the view is transformed.
    in this case use the AnchorUnderMouse enum value.


    Qt Code:
    1. CmwGraphicsView::CmwGraphicsView(QWidget *parent) :
    2. QGraphicsView(parent)
    3. {
    4. setResizeAnchor(QGraphicsView::AnchorUnderMouse); // anchir under the cursor
    5. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: How to zooming like in AutoCAD with QGraphicsView?

    Quote Originally Posted by SixDegrees View Post
    One of the QGraphicsView tutorials has wheel zooming, along with the code. I don't recall which one, but it works fine for my purposes.
    Thanks, I found example you mentioned. In "Elastic Nodes" example used setTransformationAnchor(QGraphicsView::AnchorUnder Mouse). It works fine only when two scrollbars appears. But if only one scrollbar or no any scrollbars shown zooming under mouse cursor not working. I tried disable scrollbars (setting scrollbars' policy to Qt::ScrollBarAlwaysOff), result was same.
    In my window constructor i'm creating view and setting scene rect (via QGraphicsView::setSceneRect(0,0,640,460) ).
    Quote Originally Posted by ecanela View Post
    for zooming under the mouse cursor, use the function setResizeAnchor. this function set the possible anchors that QGraphicsView can use when the user resizes the view or when the view is transformed.
    in this case use the AnchorUnderMouse enum value.

    Qt Code:
    1. CmwGraphicsView::CmwGraphicsView(QWidget *parent) :
    2. QGraphicsView(parent)
    3. {
    4. setResizeAnchor(QGraphicsView::AnchorUnderMouse); // anchir under the cursor
    5. }
    To copy to clipboard, switch view to plain text mode 
    I tried to execute setResizeAnchor alone and with setTransformationAnchor, result from adding setResizeAnchor nothing.

  6. #6
    Join Date
    Jan 2011
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to zooming like in AutoCAD with QGraphicsView?

    Hi!
    You should reimplement the QGraphicsView::wheelEvent ( QWheelEvent * event )
    This code work fine.

    header:
    Qt Code:
    1. #include <QGraphicsView>
    2.  
    3. class GeoMapGraphicsView : public QGraphicsView
    4. {
    5. Q_OBJECT
    6. public:
    7. GeoMapGraphicsView(QWidget *parent = 0);
    8. void zoom(qreal factor, QPointF centerPoint);
    9. protected:
    10. void wheelEvent ( QWheelEvent * event );
    11. private:
    12. bool isTouched;
    13. QPointF mousePos;
    14.  
    15. };
    To copy to clipboard, switch view to plain text mode 

    in source:
    Qt Code:
    1. void GeoMapGraphicsView::wheelEvent ( QWheelEvent * e)
    2. {
    3. if (e->modifiers().testFlag(Qt::ControlModifier)){ // zoom only when CTRL key pressed
    4. int numSteps = e->delta() / 15 / 8;
    5.  
    6. if (numSteps == 0) {
    7. e->ignore();
    8. return;
    9. }
    10. qreal sc = pow(1.25, numSteps); // I use scale factor 1.25
    11. this->zoom(sc, mapToScene(e->pos()));
    12. e->accept();
    13. }
    14. }
    15.  
    16. void GeoMapGraphicsView::zoom(qreal factor, QPointF centerPoint)
    17. {
    18. scale(factor, factor);
    19. centerOn(centerPoint);
    20. }
    To copy to clipboard, switch view to plain text mode 

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

    javimoya (6th June 2011)

Similar Threads

  1. Questions about zooming QGraphicsView
    By JovianGhost in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2010, 14:54
  2. Zooming is too slow with QGraphicsView
    By learning_qt in forum Qt Programming
    Replies: 10
    Last Post: 4th December 2008, 09:23
  3. Zooming
    By td in forum Newbie
    Replies: 1
    Last Post: 26th November 2008, 13:43
  4. Zooming..
    By chethana in forum Qt Programming
    Replies: 7
    Last Post: 10th October 2007, 08:17
  5. Zooming in QGraphicsView
    By JonathanForQT4 in forum Newbie
    Replies: 3
    Last Post: 17th April 2007, 05:50

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.