Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Cutting image files to separate QPixmaps

From Wiki
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
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) {
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));
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;
This page was last modified on 11 October 2012, at 01:16.
55 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.