Unsolved Has devicePixelRatio behavior changed for 5.8?
-
I pull images into a QPixmap like so:
const qreal pixelRatio = ((QGuiApplication*)QCoreApplication::instance())->devicePixelRatio(); p = p.scaled(w*pixelRatio, h*pixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation); p.setDevicePixelRatio(pixelRatio); QIcon pixmapIcon(p);
I'm not sure if this is the perfect method, but up to Qt 5.7 it resulted in a HiDPI pixmap that I was able to use at width/pixelRatio, height/pixelRatio in the UI. Now, I end up with a fuzzy bitmap no matter what which if I change nothing is twice the size as the containing widget (and clearly not HiDPI). Has the HiDPI behavior changed fundamentally for 5.8? I didn't see any difference in the documentation.
Cheers!
-
For what it's worth, I noticed the "fuzzyness" in 5.8 even without devicePixelRatio when I create a QIcon from a QPixmap so I think the problem is
QIcon pixmapIcon(p);
P.S.
Maybe this is related: https://bugreports.qt.io/browse/QTBUG-44486 -
@VRonin that's really interesting. Oddly enough, it looks really nice and sharp on 5.5-5.7 with our method, sized properly and everything. We just scale it to the pixel ratio (in this case 2X for a HiDPI Mac), then size the container to the "points" width and height.
I do think there are some wonky things in QPixmap and QIcon so maybe our implementation worked by accident before and now needs to be done a different way. Does anyone have this consistently working?
-
@VRonin The icon size is now correct, and sharp, using the above icon+pixmap initialization along with this with the resulting icon. I call setIconToSize with the pixmap and the display w/h of the container it's going into. I don't use the @2x filename trick since this is generating the pixmap programmatically using an image the user selects, which ends up on a back end:
void SSImageButton::setIconToSize(QPixmap p, int w, int h) { QIcon icon = Utilities::getIconForPixmap(p, w, h); QSize iconSize = QSize(w, h); this->setIconSize(iconSize); this->setIcon(icon); }
where getIconForPixmap is as follows:
QIcon Utilities::getIconForPixmap(QPixmap p, int w, int h) { QRect cropRect = QRect(0, 0, p.width(), p.height()); if (p.height() > p.width()) { cropRect.setHeight(cropRect.width() * h / w); cropRect.moveTop((p.height() - cropRect.height())/2); p = p.copy(cropRect); } else if (p.height() < p.width()) { cropRect.setWidth(cropRect.height() * w / h); cropRect.moveLeft((p.width() - cropRect.width())/2); p = p.copy(cropRect); } else { // it is square, we are all set } const qreal pixelRatio = ((QGuiApplication*)QCoreApplication::instance())->devicePixelRatio(); p = p.scaled(w*pixelRatio, h*pixelRatio, Qt::KeepAspectRatio, Qt::SmoothTransformation); p.setDevicePixelRatio(pixelRatio); QIcon pixmapIcon(p); return pixmapIcon; }