Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to fit the image to the frame size and maintain the quality of the image without losing any pixel quality in QQuickpainteditems
Forum Updated to NodeBB v4.3 + New Features

How to fit the image to the frame size and maintain the quality of the image without losing any pixel quality in QQuickpainteditems

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 2 Posters 564 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.
  • K Offline
    K Offline
    kaushikv
    wrote on last edited by
    #1

    Hi Team,

    I have a custom class where i am overriding the paint method to draw an image which is quite large (5000 * 6000) pixels.
    i want to make sure the picture fits my framesize keeping the aspectratio and still maintain the quality of the image when scaled down to fit the outer rectangle that contains this custom imageviewer class:

    this is my paint method:

    QRectF bounds = boundingRect();
    QImage scaled = m_image.scaledToHeight(m_image.height());
    QPointF center = bounds.center() - scaled.rect().center();

    QPointF topLeft = bounds.topLeft();

    // Draw the image

    qreal scaleX = static_cast<qreal>(width()) / static_cast<qreal>(m_image.width());
    qreal scaleY = static_cast<qreal>(height()) / static_cast<qreal>(m_image.height());
    qreal scaleFactor = qMin(scaleX, scaleY);

    QImage resizedImage = m_image.scaledToWidth(m_image.width() * scaleFactor , Qt::SmoothTransformation);

    painter.drawImage(topLeft, resizedImage);

    what Am i doing wrong?
    or how can i can achieve this?

    K 1 Reply Last reply
    0
    • K kaushikv

      Hi Team,

      I have a custom class where i am overriding the paint method to draw an image which is quite large (5000 * 6000) pixels.
      i want to make sure the picture fits my framesize keeping the aspectratio and still maintain the quality of the image when scaled down to fit the outer rectangle that contains this custom imageviewer class:

      this is my paint method:

      QRectF bounds = boundingRect();
      QImage scaled = m_image.scaledToHeight(m_image.height());
      QPointF center = bounds.center() - scaled.rect().center();

      QPointF topLeft = bounds.topLeft();

      // Draw the image

      qreal scaleX = static_cast<qreal>(width()) / static_cast<qreal>(m_image.width());
      qreal scaleY = static_cast<qreal>(height()) / static_cast<qreal>(m_image.height());
      qreal scaleFactor = qMin(scaleX, scaleY);

      QImage resizedImage = m_image.scaledToWidth(m_image.width() * scaleFactor , Qt::SmoothTransformation);

      painter.drawImage(topLeft, resizedImage);

      what Am i doing wrong?
      or how can i can achieve this?

      K Offline
      K Offline
      kaushikv
      wrote on last edited by
      #2

      @kaushikv

      should i scale the painter instead? instead of scaling the image?

      JoeCFDJ 1 Reply Last reply
      0
      • K kaushikv

        @kaushikv

        should i scale the painter instead? instead of scaling the image?

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

        @kaushikv you will lose resolution by scaling image. If you want to keep good resolution, create the source image with format svg.

        K 2 Replies Last reply
        0
        • JoeCFDJ JoeCFD

          @kaushikv you will lose resolution by scaling image. If you want to keep good resolution, create the source image with format svg.

          K Offline
          K Offline
          kaushikv
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @kaushikv you will lose resolution by scaling image. If you want to keep good resolution, create the source image with format svg.

            K Offline
            K Offline
            kaushikv
            wrote on last edited by
            #5

            @JoeCFD @JoeCFD - yeah the problem is we, want all kind of files, .bmp and .png.

            JoeCFDJ 1 Reply Last reply
            0
            • K kaushikv

              @JoeCFD @JoeCFD - yeah the problem is we, want all kind of files, .bmp and .png.

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

              @kaushikv Qt::SmoothTransformation might be a simple linear or bilinear interpolation. Take a look at the source code.

              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