Cutting image files to separate QPixmaps
This code snippet shows how you can load an image file (for example, PNG) and separate it into several smaller QPixmap tiles for display. This is useful, for example, if you wanted to implement a slide puzzle game.
Article Metadata
Source code
QList<QPixmap*> frames; //will include the output frames
QString file = "c:/image.png";
int cols = 5;
int rows = 5;
QPixmap *image = new QPixmap(file);
if (cols == 1 && rows == 1) {
frames.append(image);
w = image->width();
h = image->height();
}
else {
int iw = image->width();
int ih = image->height();
w = iw/cols;
h = ih/rows;
int x = 0;
int y = 0;
for (int r=0; r < rows; r++) {
for (int c=0; c < cols; c++) {
QPixmap *cropped = new QPixmap(image->copy(x,y,w,h));
frames.append(cropped);
x += w;
}
y += h;
x = 0;
}
delete image;
}
//Do something with the pixmaps
//Delete in the end
if (frames.count() > 0) {
foreach (QPixmap* pixmap, frames) {
delete pixmap;
}
}
frames.clear();


Rahulvala - Add screenshot
If possible, then add the screenshot of application. So that person get idea how it convert image in several smaller tiles.rahulvala 15:46, 12 December 2011 (EET)
Hamishwillee - @Teemup
This is a pretty good article - I've subedited slightly and added Graphics category to make it easier to find and also added a link to the QPixmap API reference in the Abstract. It would be cool to know if there is any convenient way to do this in Qt Quick since that is recommended for UI development now?
Would it be possible to add the sdk, devices, platforms that this is compatible with/you tested against in the ArticleMetaData template? This helps other users know whether the article is still relevant to them as time goes by.
FYI, when we review articles we do so against this checklist: Help:Wiki Article Review Checklist. Other things you might do to improve the article (all optional of course) are:
hamishwillee 00:34, 13 December 2011 (EET)
Teemup -
I will try to make a simple enough Qt application and have screenshots of the functionality.teemup 12:42, 13 December 2011 (EET)