Solved Pixmap not scaling on hdpi
-
That works on pixmaps generated from QIcon (mostly, there are still some with issues), but it only very slightly increases the size from pixmaps generated from Qicon::fromTheme.
QPixmap generateOpacity(QPixmap &input) { QPixmap normalPixmap(input); normalPixmap.setDevicePixelRatio(getDevPixRatio()); QPixmap disabledPixmap(normalPixmap.size()); disabledPixmap.setDevicePixelRatio(getDevPixRatio()); disabledPixmap.fill(Qt::transparent); QPainter p(&disabledPixmap); p.setBackgroundMode(Qt::TransparentMode); p.setBackground(QBrush(Qt::transparent)); p.eraseRect(normalPixmap.rect()); p.setOpacity(0.2); p.drawPixmap(0, 0, normalPixmap); p.end(); return disabledPixmap; }
-
-
@Christian-Ehrlicher Oh, forgive me for missing that out.
int getDevPixRatio() { static int devPixRatio = QApplication::desktop()->devicePixelRatio(); return devPixRatio; }
-
You forgot to multiply the pixel size of the pixmap with the device pixel ratio:
For example, painting on a 200x200 image if with a ratio of 2.0 will result in effective (device-independent) painting bounds of 100x100.
-
@Christian-Ehrlicher Hmm, I tried this but it produces corrupted looking pixmaps, most of them display nothing. Is this right?
QPixmap generateOpacity(QPixmap &input) { qreal ratio = getDevPixRatio(); QPixmap disabledPixmap(input.width() * ratio, input.height() * ratio); disabledPixmap.setDevicePixelRatio(ratio); disabledPixmap.fill(Qt::transparent); QPixmap normalPixmap(input.width() * ratio, input.height() * ratio); normalPixmap.setDevicePixelRatio(ratio); QPainter p(&disabledPixmap); p.setBackgroundMode(Qt::TransparentMode); p.setBackground(QBrush(Qt::transparent)); p.eraseRect(normalPixmap.rect()); p.setOpacity(0.2); p.drawPixmap(0, 0, normalPixmap); p.end(); return disabledPixmap; }
-
@Kite-R said in Pixmap not scaling on hdpi:
most of them display nothing. Is this right?
QPixmap normalPixmap(input.width() * ratio, input.height() * ratio);
You create an empty pixmap...
-
@Christian-Ehrlicher Setting this does the same as before, tiny pixmaps.
QPixmap normalPixmap(input); normalPixmap.setDevicePixelRatio(ratio);
-
@Kite-R said in Pixmap not scaling on hdpi:
Setting this does the same as before, tiny pixmaps.
You have to scale it up - otherwise you only create a big pixmap with a small icon inside.
-
@Christian-Ehrlicher Aaah... thanks so much man, it's been a long day, this worked:
QPixmap normalPixmap = input.scaled(input.width() * ratio, input.height() * ratio, Qt::KeepAspectRatio);
-
Yeah, the high-dpi stuff is not easy... even in the qt styles there are still a lot of pitfalls, esp. due to rounding errors.