Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsview zooming under mouse cursor
Forum Update on Monday, May 27th 2025

QGraphicsview zooming under mouse cursor

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wejgomi
    wrote on last edited by wejgomi
    #1

    Hi,

    I am using a basic code for QGraphicsview's mouse wheel event for zooming in & out :

    if (event->angleDelta().y() > 0)
    {
    	scale(1.25, 1.25);
    }
    else
    {
    	scale(0.8, 0.8);
    }
    

    However that way it zooms by ignoring where the mouse cursor is , but instead where the scene rect is as far as I can understand.

    For ex: when you zoom in in GoogleMaps, you will notice that it will also be changing the "sceneRect" and zoom into the area under the mouse cursor.

    I have tried changing the scene rect as well based on cursor position but it didn't give the affect that I was looking for. The change in the view was too much and quickly went beyond the canvas before wheel event.

    Wanted to ask if in case someone dealt with this before.

    Many thanks

    JonBJ Ketan__Patel__0011K 2 Replies Last reply
    0
    • W wejgomi

      Hi,

      I am using a basic code for QGraphicsview's mouse wheel event for zooming in & out :

      if (event->angleDelta().y() > 0)
      {
      	scale(1.25, 1.25);
      }
      else
      {
      	scale(0.8, 0.8);
      }
      

      However that way it zooms by ignoring where the mouse cursor is , but instead where the scene rect is as far as I can understand.

      For ex: when you zoom in in GoogleMaps, you will notice that it will also be changing the "sceneRect" and zoom into the area under the mouse cursor.

      I have tried changing the scene rect as well based on cursor position but it didn't give the affect that I was looking for. The change in the view was too much and quickly went beyond the canvas before wheel event.

      Wanted to ask if in case someone dealt with this before.

      Many thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @wejgomi said in QGraphicsview zooming under mouse cursor:

      However that way it zooms by ignoring where the mouse cursor is

      You need to call setTransformationAnchor(QGraphicsView::AnchorUnderMouse) just before scale() to have it keep the mouse "anchored".

      Here is an example I found in old projects of mine, illustrating this. I haven't analyzed it further, but it might give you an idea how I handled it:

      /*virtual*/ void MapWindowGraphicsView::wheelEvent(QWheelEvent *event) /*override*/
      {
          // zoom in/out
          // ensure we deal with with wheel event
          event->accept();
          // set delta scale and the resulting scaling
          qreal deltaScale = 1.0;
          deltaScale += (event->angleDelta().y() > 0) ? 0.1 : -0.1;
          qreal horizontalScale = transform().m11() * deltaScale;
          qreal verticalScale = transform().m22() * deltaScale;
          Q_ASSERT(qFuzzyCompare(horizontalScale, verticalScale));
          // get the view size
          QSize viewSize(rect().size());
          // get the new scaled scence size
          QSizeF sceneSize(sceneRect().size());
          sceneSize *= horizontalScale;
          // if it's about the size of the view, or smaller, do a `fitInView()` (limit zoom in)
          if (sceneSize.width() <= viewSize.width() && sceneSize.height() <= viewSize.height())
          {
              doFitInView(true);
              return;
          }
          // ignore if too large (limit zoom out)
          if (sceneSize.width() > viewSize.width() * 3 || sceneSize.height() > viewSize.height() * 3)
              return;
          doFitInView(false);
          // do the zoom
          setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
          scale(deltaScale, deltaScale);
      }
      
      W 1 Reply Last reply
      2
      • JonBJ JonB

        @wejgomi said in QGraphicsview zooming under mouse cursor:

        However that way it zooms by ignoring where the mouse cursor is

        You need to call setTransformationAnchor(QGraphicsView::AnchorUnderMouse) just before scale() to have it keep the mouse "anchored".

        Here is an example I found in old projects of mine, illustrating this. I haven't analyzed it further, but it might give you an idea how I handled it:

        /*virtual*/ void MapWindowGraphicsView::wheelEvent(QWheelEvent *event) /*override*/
        {
            // zoom in/out
            // ensure we deal with with wheel event
            event->accept();
            // set delta scale and the resulting scaling
            qreal deltaScale = 1.0;
            deltaScale += (event->angleDelta().y() > 0) ? 0.1 : -0.1;
            qreal horizontalScale = transform().m11() * deltaScale;
            qreal verticalScale = transform().m22() * deltaScale;
            Q_ASSERT(qFuzzyCompare(horizontalScale, verticalScale));
            // get the view size
            QSize viewSize(rect().size());
            // get the new scaled scence size
            QSizeF sceneSize(sceneRect().size());
            sceneSize *= horizontalScale;
            // if it's about the size of the view, or smaller, do a `fitInView()` (limit zoom in)
            if (sceneSize.width() <= viewSize.width() && sceneSize.height() <= viewSize.height())
            {
                doFitInView(true);
                return;
            }
            // ignore if too large (limit zoom out)
            if (sceneSize.width() > viewSize.width() * 3 || sceneSize.height() > viewSize.height() * 3)
                return;
            doFitInView(false);
            // do the zoom
            setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
            scale(deltaScale, deltaScale);
        }
        
        W Offline
        W Offline
        wejgomi
        wrote on last edited by
        #3

        @JonB Many thanks for the answer.

        1 Reply Last reply
        0
        • W wejgomi

          Hi,

          I am using a basic code for QGraphicsview's mouse wheel event for zooming in & out :

          if (event->angleDelta().y() > 0)
          {
          	scale(1.25, 1.25);
          }
          else
          {
          	scale(0.8, 0.8);
          }
          

          However that way it zooms by ignoring where the mouse cursor is , but instead where the scene rect is as far as I can understand.

          For ex: when you zoom in in GoogleMaps, you will notice that it will also be changing the "sceneRect" and zoom into the area under the mouse cursor.

          I have tried changing the scene rect as well based on cursor position but it didn't give the affect that I was looking for. The change in the view was too much and quickly went beyond the canvas before wheel event.

          Wanted to ask if in case someone dealt with this before.

          Many thanks

          Ketan__Patel__0011K Offline
          Ketan__Patel__0011K Offline
          Ketan__Patel__0011
          wrote on last edited by
          #4

          @wejgomi

          You need To set TransformationAnchor before calling the scale function.

          setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
          

          void Custome_View::wheelEvent(QWheelEvent *event)
          {
          setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
          double ScaleFactor = 1.15;

          if(event->delta() > 0)
          {
              scale(ScaleFactor,ScaleFactor);
          }
          else
          {
              scale(1/ScaleFactor,1/ScaleFactor);
          }
          

          }

          JonBJ 1 Reply Last reply
          0
          • Ketan__Patel__0011K Ketan__Patel__0011

            @wejgomi

            You need To set TransformationAnchor before calling the scale function.

            setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
            

            void Custome_View::wheelEvent(QWheelEvent *event)
            {
            setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
            double ScaleFactor = 1.15;

            if(event->delta() > 0)
            {
                scale(ScaleFactor,ScaleFactor);
            }
            else
            {
                scale(1/ScaleFactor,1/ScaleFactor);
            }
            

            }

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @Ketan__Patel__0011 said in QGraphicsview zooming under mouse cursor:

            You need To set TransformationAnchor before calling the scale function.

            You mean exactly as I wrote above

            You need to call setTransformationAnchor(QGraphicsView::AnchorUnderMouse) just before scale() to have it keep the mouse "anchored".

            and my code which shows it being used...? :)

            Ketan__Patel__0011K 1 Reply Last reply
            0
            • JonBJ JonB

              @Ketan__Patel__0011 said in QGraphicsview zooming under mouse cursor:

              You need To set TransformationAnchor before calling the scale function.

              You mean exactly as I wrote above

              You need to call setTransformationAnchor(QGraphicsView::AnchorUnderMouse) just before scale() to have it keep the mouse "anchored".

              and my code which shows it being used...? :)

              Ketan__Patel__0011K Offline
              Ketan__Patel__0011K Offline
              Ketan__Patel__0011
              wrote on last edited by
              #6

              @JonB
              Well my code is also is in used and it giving the perfect result and working very well.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved