First of all, there is no need to allocate QPixmaps on the heap. It's an implicitly shared class:
// QPixmap* originalPx;
// QPixmap* newPx;
originalPx
= QPixmap::fromImage(img
);
newPx
= QPixmap(width
(), height
());
// QPixmap* originalPx;
// QPixmap* newPx;
QPixmap originalPx;
QPixmap newPx;
originalPx = QPixmap::fromImage(img);
newPx = QPixmap(width(), height());
To copy to clipboard, switch view to plain text mode
Notice that QPixmap::scaled() RETURNS the scaled pixmap and you might want to always scale the original pixmap not to lose information over time:
newPx = originalPx.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
newPx = originalPx.scaled(size, Qt::IgnoreAspectRatio, Qt::FastTransformation);
To copy to clipboard, switch view to plain text mode
Because you ignore aspect ratio and the pixmap size always matches widget size, you can just draw the pixmap in the upper left corner (0,0):
painter.drawPixmap(0, 0, newPx);
painter.drawPixmap(0, 0, newPx);
To copy to clipboard, switch view to plain text mode
If you later decide to keep aspect ratio, then you can use this handy method in QStyle to draw the pixmap as aligned:
style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, newPx);
style()->drawItemPixmap(&painter, rect(), Qt::AlignCenter, newPx);
To copy to clipboard, switch view to plain text mode
Bookmarks