(Qt C++) Resize pixmap and KEEP pixelation?

mrg95 picture mrg95 · Jul 17, 2013 · Viewed 9.9k times · Source

In my project I have a QLabel that I change the pixmap frequently like this:

ui->frameLabel->setPixmap(slot_pic[blockId[currentSlot]][damageId[currentSlot]]);

slot_pic is simply a 2d map. So you can look at it clearer like this:

ui->frameLabel->setPixmap(pixmap);

The image is 16x16 in size and my label is 32x32. I have scaledContents checked so when the pixmap changes, the image is double in size. However, the image is now blurry. I understand why, but I was wondering if there is a way to make it stay pixelated. I want to just have a bigger pixelated image. (The image is from Minecraft if that helps you understand what I mean)

Thanks for your time :)

Answer

user362638 picture user362638 · Jul 17, 2013

Don't let the QLabel do the scaling. Instead, do the scaling by yourself using QPixmap::scaled(). Something like this:

ui->frameLabel->setPixmap(
    pixmap.scaled(32, 32, Qt::IgnoreAspectRatio, Qt::FastTransformation));

The important parameter is the last one, transformMode, which tells whether bilinear filtering is used or not.