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

Draw smoothly scaled image using QPainter and OpenGLWidget?



  • I'm trying to smoothly scale down a QImage inside OpenGLWidget (being drawing with a QPainter) but it looks really bad i.e. text becomes unreadable etc. What's the most efficient way of scaling image in Qt?



  • Found a solution, basically created a 2D Texture with same image format. setData is working now without any issue.


  • Lifetime Qt Champion

    Hi,

    Can you share the code you are using ?
    What is the size of the original image ?
    What is the target size ?



  • @SGaist Here's the paintEvent method:

    QPainter painter;
    painter.begin(this);
    painter.setRenderHint(QPainter::Antialiasing);
    	
    if (image)
    {
    	QRect wndRect = this->rect();
            painter.drawImage(wndRect, *image, image->rect());
    }
    
    painter.end();
    

    Original Image size: 1920 x 1080
    Widget size: 960 x 540 (50% of original size)


  • Lifetime Qt Champion

    Did you check QImage::scaled ?



  • @SGaist Yes but its too slow and SmoothTransformation setting creates a blurry looking image - when there is text.

    Was hoping for OpenGL to take care of scaling etc.


  • Lifetime Qt Champion

    Then you have to make a texture of your image and draw it directly with OpenGL.



  • @Taytoo Always better to use svg as original image type and scale it from there.
    And use Qt::KeepAspectRatio or try to make height for width or width for height.



  • @SGaist Just got it working via OpenGL using an example I found online. The problem now is that when I try to update the image by calling setData on texture, then I get following messages in debugger, however, the display is updated without any issues.

    QOpenGLTexture::setFormat(): Cannot change format once storage has been allocated
    Cannot resize a texture that already has storage allocated.
    To do so, destroy() the texture and then create() and setSize()
    Cannot set mip levels on a texture that already has storage allocated.
    To do so, destroy() the texture and then create() and setMipLevels()

    I'm updating Texture using code:

    texture->setData(image, QOpenGLTexture::MipMapGeneration::DontGenerateMipMaps);
    

    An image with same RGB32 format and same width/height was used to create the texture, so whats the reason for warnings above?



  • Found a solution, basically created a 2D Texture with same image format. setData is working now without any issue.


Log in to reply