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. zoom in/out on a qwidget
Qt 6.11 is out! See what's new in the release blog

zoom in/out on a qwidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 2 Posters 5.3k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rest
    wrote on last edited by
    #1

    hi! i'm trying to zoom on a widget were i draw lines/curves.
    and my problem is that it zooms just on the top left . it could be something with the resize of the widget?

    my code is like this:

    void DrawShape::wheelEvent(QWheelEvent *event)
    {

    const int degrees = event->delta() / 8;
    
    int steps = degrees / 15;
    double scaleFactor = 1.0;
    const qreal minFactor = 1.0;
    const qreal maxFactor = 10.0;
    
    
    
    
        if(steps > 0)
        {
            m_ScaleFactor.scaleX = (m_ScaleFactor.scaleX >= maxFactor) ? m_ScaleFactor.scaleX : (m_ScaleFactor.scaleX + scaleFactor);
            m_ScaleFactor.scaleY = (m_ScaleFactor.scaleY >= maxFactor) ? m_ScaleFactor.scaleY : (m_ScaleFactor.scaleY + scaleFactor);
        }
        else
        {
            m_ScaleFactor.scaleX = (m_ScaleFactor.scaleX <= minFactor) ? minFactor : (m_ScaleFactor.scaleX - scaleFactor);
            m_ScaleFactor.scaleY = (m_ScaleFactor.scaleY <= minFactor) ? minFactor : (m_ScaleFactor.scaleY - scaleFactor);
        }
    
    
    
    update();
    

    }

    where m_ScaleFactor is a struct

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      How do you apply the scale values?
      Or do u mean, you zoom the actual drawing in paintEvent and not the widget it self?

      R 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        How do you apply the scale values?
        Or do u mean, you zoom the actual drawing in paintEvent and not the widget it self?

        R Offline
        R Offline
        rest
        wrote on last edited by
        #3

        @mrjj i zoom in the paintEvent

        mrjjM 1 Reply Last reply
        0
        • R rest

          @mrjj i zoom in the paintEvent

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rest
          ok, must be something in the draw code then :)
          Can you show paintEvent.

          Also you say it only zoom in top left corner.
          Can you tell more about that?

          R 1 Reply Last reply
          0
          • R Offline
            R Offline
            rest
            wrote on last edited by rest
            #5

            void DrawShape::paintEvent(QPaintEvent *)
            {
            // we use this to make the background white
            QStyleOption option;
            option.init(this);
            QPainter painter(this);
            style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);

            painter.setPen(QPen(Qt::black, 7));
            // we draw all the current points
            for(int i = 0; i < shapeWeDrawNow.getAllPoints().size(); i++)
                painter.drawPoint(shapeWeDrawNow.getAllPoints()[i].getX(), shapeWeDrawNow.getAllPoints()[i].getY());
            
            painter.setPen(QPen(Qt::black, 2));
            // we draw all the current lines
            for(int i = 1; i < shapeWeDrawNow.getAllPoints().size(); i++)
                if(shapeWeDrawNow.getAllPoints()[i].getType() == 'l' && shapeWeDrawNow.getAllPoints()[i-1].getType() != 'b')
                    painter.drawLine(shapeWeDrawNow.getAllPoints()[i-1].getX(), shapeWeDrawNow.getAllPoints()[i-1].getY(), shapeWeDrawNow.getAllPoints()[i].getX(), shapeWeDrawNow.getAllPoints()[i].getY());
                else if(i >= 3 && shapeWeDrawNow.getAllPoints()[i-2].getType() == 'b' && shapeWeDrawNow.getAllPoints()[i-1].getType() == 'b')
                {
                    QPainterPath path;
                    path.moveTo(shapeWeDrawNow.getAllPoints()[i-3].getX(), shapeWeDrawNow.getAllPoints()[i-3].getY());
                    path.cubicTo(shapeWeDrawNow.getAllPoints()[i-2].getX(), shapeWeDrawNow.getAllPoints()[i-2].getY(), shapeWeDrawNow.getAllPoints()[i-1].getX(), shapeWeDrawNow.getAllPoints()[i-1].getY(), shapeWeDrawNow.getAllPoints()[i].getX(), shapeWeDrawNow.getAllPoints()[i].getY());
                    painter.drawPath(path);
                }
            
            painter.scale(m_ScaleFactor.scaleX, m_ScaleFactor.scaleY);
            

            }

            1 Reply Last reply
            0
            • mrjjM mrjj

              @rest
              ok, must be something in the draw code then :)
              Can you show paintEvent.

              Also you say it only zoom in top left corner.
              Can you tell more about that?

              R Offline
              R Offline
              rest
              wrote on last edited by
              #6

              @mrjj is like he zooms just on the top and i would like it to zoom on the mouse position

              mrjjM 1 Reply Last reply
              0
              • R rest

                @mrjj is like he zooms just on the top and i would like it to zoom on the mouse position

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @rest
                Hi
                http://doc.qt.io/qt-5/qtwidgets-painting-transformations-example.html

                Think you need to call .translate before you call .scale. so you move the origin from
                topleft corner to mouse. Make sure the mouse position is already converted from global to local before used.

                R 1 Reply Last reply
                0
                • mrjjM mrjj

                  @rest
                  Hi
                  http://doc.qt.io/qt-5/qtwidgets-painting-transformations-example.html

                  Think you need to call .translate before you call .scale. so you move the origin from
                  topleft corner to mouse. Make sure the mouse position is already converted from global to local before used.

                  R Offline
                  R Offline
                  rest
                  wrote on last edited by
                  #8

                  @mrjj thanks, solved somehow, but now i have the problem that when i make another object, the first comes after him

                  mrjjM 1 Reply Last reply
                  0
                  • R rest

                    @mrjj thanks, solved somehow, but now i have the problem that when i make another object, the first comes after him

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @rest
                    Well did you try to reset the translate before adding a new object ?

                    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