(Qt C++) Resize pixmap and KEEP pixelation?
Asked Answered
A

1

10

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 :)

Anacreontic answered 17/7, 2013 at 6:43 Comment(2)
There are some RenderHints that you can set in a QPainter. There you can disable antialiasing. I'm not sure on how to get the QLabel::paintEvent to use this flag.Avarice
I've never messed with QPainter so I'll have to look into it. The pixmap isn't being created in my program, just so you know. Its just a resource png file. I have one smaller image and when you click it, a label shows the same image but bigger.Anacreontic
M
21

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.

Mozambique answered 17/7, 2013 at 7:32 Comment(2)
This worked PERFECTLY. Zero errors and was simple. Thank you :)Anacreontic
Thanks for the nice answer. I know about this and was thinking of employing this technique, but I'm looking for something which can be set directly in the Qt Designer (if possible) using the style-sheet! Anything like that? Anyone?Spark

© 2022 - 2024 — McMap. All rights reserved.