Disabling antialiasing when drawing fonts in Qt 4.8.x

When you draw text in Qt using QPainter and desire it to be pixel crisp and sharp, you might do something like this:

QPainter painter(&this);
painter.setRenderHint(QPainter::TextAntialiasing, false);
painter.drawText(this.rect(), "Some Text");
Qt 4.8.x QPainter drawing text with default anti-aliasing on
Qt 4.8.x QPainter drawing text with default anti-aliasing on

However, sometimes it doesn’t work. Your text might be drawn with antialiasing no matter what, which results in soft blurry sometimes ugly font.

So try this:

QFont font;
font.setFamily("SomeFontFamily");
font.setStyleStrategy(QFont::NoAntialias);

painter.setFont(font);
Qt 4.8.x QPainter drawing text with default anti-aliasing off, which requires finding the right function
Qt 4.8.x QPainter drawing text with default anti-aliasing off, which requires finding the right function

Presto! Your font should be drawn all blocky and pixel retro style with no antialiasing. If that doesn’t work, you might want to pre-render your font characters as sprites and draw them as QImages or QPixmaps.

NOTE: If you’re using an Internet browser like Chrome, it might apply anti-aliasing on the reference images, so the image demonstrating anti-aliasing off won’t look correct. You’ll have to disable anti-aliasing in your Internet browser or download and view the image with an image viewer that will display it correctly. I attempted to disable antialiasing using CSS though.