Namespaces
Variants
Actions
Revision as of 04:16, 11 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Cutting image files to separate QPixmaps

Jump to: navigation, search

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

Article
Keywords: QPixmap
Created: teemup (12 Dec 2011)
Last edited: hamishwillee (11 Oct 2012)

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();
244 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved