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

mrg95
  • 2,371
  • 11
  • 46
  • 89
  • 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. – Ralph Tandetzky Jul 17 '13 at 07:25
  • 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. – mrg95 Jul 17 '13 at 07:29

1 Answers1

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.

  • This worked PERFECTLY. Zero errors and was simple. Thank you :) – mrg95 Jul 17 '13 at 07:37
  • 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? – zeFree Oct 22 '13 at 02:30