zoom in/out on a qwidget

  • 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);
            m_ScaleFactor.scaleX = (m_ScaleFactor.scaleX <= minFactor) ? minFactor : (m_ScaleFactor.scaleX - scaleFactor);
            m_ScaleFactor.scaleY = (m_ScaleFactor.scaleY <= minFactor) ? minFactor : (m_ScaleFactor.scaleY - scaleFactor);


    where m_ScaleFactor is a struct

  • Qt Champions 2016

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

  • @mrjj i zoom in the paintEvent

  • Qt Champions 2016

    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?

  • void DrawShape::paintEvent(QPaintEvent *)
    // we use this to make the background white
    QStyleOption option;
    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.scale(m_ScaleFactor.scaleX, m_ScaleFactor.scaleY);


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

  • Qt Champions 2016


    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.

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

  • Qt Champions 2016

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.