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 setScale(qreal zoom)
QtWS25 Last Chance

QGraphicsView setScale(qreal zoom)

Scheduled Pinned Locked Moved General and Desktop
10 Posts 2 Posters 22.0k 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.
  • I Offline
    I Offline
    IrQX
    wrote on last edited by
    #1

    Hi all!

    I have a problem, now i writing a program, that uses QGraphicsView.
    View has Zooming functionality

    @void MainView::scaleBy(double factor)
    {
    scale(factor, factor);
    }

    void MainView::wheelEvent(QWheelEvent *event)
    {
    if (event->modifiers() == Qt::ControlModifier)
    {
    scaleBy(std::pow(4.0 / 3.0, (-event->delta() / 240.0)));
    }
    else
    {
    QGraphicsView::wheelEvent(event);
    }
    }

    void MainView::zoomIn()
    {
    scaleBy(1.1);
    }

    void MainView::zoomOut()
    {
    scaleBy(1.0 / 1.1);
    }@

    but I want to have an limited zoom, also a tool to set fixed scale (such 100%, 150%, etc). I can't implement a function like this, QGraphicsView::setScale(qreal zoom), because I need current scale value. Anybody can say me, how I can get it?

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on last edited by
      #2

      i think u can have the original scale factor and current scale factor stored as variables. compute the current scale (100, 150, 200) always based on the original scale. if you don't do this, you might geometrically scale the image to a very high factor

      1 Reply Last reply
      0
      • I Offline
        I Offline
        IrQX
        wrote on last edited by
        #3

        [quote author="chetankjain" date="1281335356"]i think u can have the original scale factor and current scale factor stored as variables. compute the current scale (100, 150, 200) always based on the original scale. if you don't do this, you might geometrically scale the image to a very high factor[/quote]

        Look, I want insert check for minimum and maximum scale value to MainView::scaleBy(double factor),

        @if (cur_value * factor > MAXIMUM)
        {
        cur_value = MAXIMUM;
        setScale(MAXIMUM);
        }
        else
        scale(factor, factor);@

        But I can't do this, because I haven't setScale(), and I also havn't cur_value. Not sure that store it as class-member is a good idea.

        1 Reply Last reply
        0
        • I Offline
          I Offline
          IrQX
          wrote on last edited by
          #4

          I solved my problem, but I had used storing scale value as class-member variable. If anyone knows more beauty way, please write it here.

          @void MainView::scaleBy(double factor)
          {
          m_curScaleFactor *= factor;
          scale(factor, factor);
          }

          void MainView::scaleView(qreal scaleFactor)
          {
          if (((m_curScaleFactor == m_minScale) && (scaleFactor < 1.0)) ||
          ((m_curScaleFactor == m_maxScale) && (scaleFactor > 1.0))) return;

          qreal sc = scaleFactor;
          
          if ((m_curScaleFactor * sc < (m_minScale))&&(sc < 1.0))
          {
              sc = m_minScale / m_curScaleFactor;
          }
          else
              if ((m_curScaleFactor * sc > m_maxScale)&&(sc > 1.0))
              {
              sc = m_maxScale / m_curScaleFactor;
          }
          scaleBy(sc);
          

          }

          void MainView::setZoom(int percentZoom)
          {
          qreal targetScale = (qreal)percentZoom / 100.0;
          qreal scaleFactor = targetScale / m_curScaleFactor;
          scaleBy(scaleFactor);
          }@

          1 Reply Last reply
          0
          • ? This user is from outside of this forum
            ? This user is from outside of this forum
            Guest
            wrote on last edited by
            #5

            for a graphicsview, i think you can get the current transform and then the m11 and m22 elements would specify the current horizontal and vertical scaling. the defaults would be 1.0. would this suffice ?

            1 Reply Last reply
            0
            • I Offline
              I Offline
              IrQX
              wrote on last edited by
              #6

              [quote author="chetankjain" date="1281338119"]for a graphicsview, i think you can get the current transform and then the m11 and m22 elements would specify the current horizontal and vertical scaling. the defaults would be 1.0. would this suffice ?
              [/quote]

              thankyou! it works fine.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tobias.hunger
                wrote on last edited by
                #7

                Is @scale()@ the method you need? The documentation suggests that it returns the value previously set by @setScale(...)@.

                1 Reply Last reply
                0
                • ? This user is from outside of this forum
                  ? This user is from outside of this forum
                  Guest
                  wrote on last edited by
                  #8

                  @Tobias, scale() would also directly scale the graphicsview and return the Transform, using which you could then find the matrix elements. if the view is already scaled, then you have to obtain the transform first

                  @IrQX glad it worked :)

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    IrQX
                    wrote on last edited by
                    #9

                    [quote author="Tobias Hunger" date="1281342135"]Is @scale()@ the method you need? The documentation suggests that it returns the value previously set by @setScale(...)@.[/quote]

                    QGraphicsView haven't whis method (4.6.2).

                    @void scale(qreal sx, qreal sy)@
                    Scales the current view transformation by (sx, sy).

                    1 Reply Last reply
                    0
                    • I Offline
                      I Offline
                      IrQX
                      wrote on last edited by
                      #10

                      So, if it will be useful for anybody:
                      @
                      // in *.h
                      inline qreal curScale() const {return transform().m11();}

                      // in *.cpp
                      void MainView::scaleBy(qreal scaleFactor)
                      {
                      qreal curScaleFactor = transform().m11();
                      if (((curScaleFactor == minScale) && (scaleFactor < 1.0)) ||
                      ((curScaleFactor == maxScale) && (scaleFactor > 1.0))) return;

                      qreal sc = scaleFactor;
                      
                      if ((curScaleFactor * sc < minScale)&&(sc < 1.0))
                      {
                          sc = minScale / curScaleFactor;
                      }
                      else
                          if ((curScaleFactor * sc > maxScale)&&(sc > 1.0))
                          {
                          sc = maxScale / curScaleFactor;
                      }
                      scale(sc, sc);
                      

                      }

                      void MainView::setZoom(int percentZoom)
                      {
                      qreal targetScale = (qreal)percentZoom / 100.0;
                      qreal scaleFactor = targetScale / transform().m11();
                      scaleBy(scaleFactor);
                      }@

                      I used only m11, cause in my case m11 == m22.

                      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