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. Draw smoothly scaled image using QPainter and OpenGLWidget?

Draw smoothly scaled image using QPainter and OpenGLWidget?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.9k Views 2 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.
  • T Offline
    T Offline
    Taytoo
    wrote on last edited by Taytoo
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Taytoo
      wrote on last edited by
      #9

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

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        T 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

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

          T Offline
          T Offline
          Taytoo
          wrote on last edited by
          #3

          @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)

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Did you check QImage::scaled ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            T 1 Reply Last reply
            0
            • SGaistS SGaist

              Did you check QImage::scaled ?

              T Offline
              T Offline
              Taytoo
              wrote on last edited by
              #5

              @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.

              JoeCFDJ 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply
                2
                • T Taytoo

                  @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.

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #7

                  @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.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

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

                    T Offline
                    T Offline
                    Taytoo
                    wrote on last edited by
                    #8

                    @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?

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      Taytoo
                      wrote on last edited by
                      #9

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

                      1 Reply Last reply
                      3

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved