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. Is there any way to smoothly scale and draw a section of a QPixmap that's defined by a QRectF?

Is there any way to smoothly scale and draw a section of a QPixmap that's defined by a QRectF?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpixmapqpainterscalingqrectf
3 Posts 2 Posters 886 Views
  • 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.
  • G Offline
    G Offline
    Guy Gizmo
    wrote on 9 Mar 2022, 16:31 last edited by Guy Gizmo 3 Sept 2022, 16:33
    #1

    I'm working on a Qt 5 mac application and I'm looking for a method to smoothly scale and render a fractional section of a QPixmap as defined by a QRectF.

    What I want to do actually is exactly what void QPainter::drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect) is supposed to do:

    QRectF sourceRect = ...;
    QRectF targetRect = ...;
    painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing);
    painter.drawPixmap(targetRect, pixmap, sourceRect);
    

    However, as mentioned in this thread and this bug report, QPainter::SmoothPixmapTransform is ignored and this method does not work.

    Short of writing my own efficient bilinear scaling method from scratch (which I am not inclined to do), is there any other method I can use to accomplish the same thing?

    QPixmap::scaled() only allows scaling to integer dimensions, and provides no way to crop out a section defined by a QRectF, so that's a non-starter.

    Just tossing out ideas here, but is it possible to somehow use OpenGL to render the portion of the image I want at the right scale and then render it back into a QImage or QPixmap? This kind of scaling is one of the most basic things OpenGL can do very easily and quickly, but I don't have any experience using OpenGL in a Qt desktop app so far so I'm not sure if this is possible.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 9 Mar 2022, 17:01 last edited by
      #2

      Hi,
      Don't know if it meets your use case, but you can try to apply a blur effect, here the code I used to create a thumbnail from a web page:

      QImage TopSitesManager::blurImage(const QImage& img)
      {
      	if(img.isNull()) return img;
      
      	QGraphicsScene scene;
      	QGraphicsPixmapItem item;
      	QGraphicsBlurEffect effect;
      
      	effect.setBlurRadius(3.0);
      	effect.setBlurHints(QGraphicsBlurEffect::QualityHint);
      	item.setPixmap(QPixmap::fromImage(img));
      	item.setGraphicsEffect(&effect);
      	scene.addItem(&item);
      	QImage blurImg(img.size(),QImage::Format_ARGB32);
      	blurImg.fill(Qt::transparent);
      
      	QPainter painter(&blurImg);
      	QRectF source(0,0,img.size().width(),img.size().height());
      	scene.render(&painter,source,source);
          painter.end();
      
      	return blurImg;
      }
      
      1 Reply Last reply
      1
      • G Offline
        G Offline
        Guy Gizmo
        wrote on 10 Mar 2022, 17:59 last edited by
        #3

        I'm afraid that doesn't help for my use case, since I need the images to appear as clear and clean as possible when being scaled down.

        1 Reply Last reply
        0

        3/3

        10 Mar 2022, 17:59

        • Login

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