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