Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I scale an image with QPainter?



  • I have an image stored as QPixmap and I want to draw it out with QPainter and scale it. I overloaded the QPaintEvent in my widget and drew the QPixmap, but I don't know how to get it to scale.

    void ImagePainter::paintEvent(QPaintEvent *a_pEvent)
    {
        QPainter painter(this);
        painter.drawPixmap(10,10, m_image.width(), m_image.height(), m_image);
        painter.scale(1000,800);
        painter.end();    
    }
    

    The scale function in here didn't do anything.



  • Scale first, then draw. Or you could also just do like

    painter.drawPixmap(10,10, m_image.width() * 1000, m_image.height() * 800, m_image);
    

    You may also call painter.setRenderHint(QPainter::SmoothPixmapTransform) before drawing any pixmap to make it scales more smoothly.



  • @Bonnie

    Thank you, setting it before drawPixmap works. I don't need any smoothing so nearest neighbor resize is fine for me.


Log in to reply