Abhay Salvi
Abhay Salvi

Reputation: 1099

Why fonts in Qt are appearing blurry or pixelated?

All my fonts are appearing pixelated, so I used AntiAliasing but it isn't helping out. As you can see the pixelated font in the image itself:

enter image description here

This is the code I am currently using:

butt1 = QtWidgets.QLabel("""Scrappr""")
font = QtGui.QFont()
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
font.setPixelSize(22)
font.setFamily('Segoe UI Bold')
butt1.setFont(QtGui.QFont(font))

I tried different solutions on SO, qtforums etc but nothing works for me :(

I tried:

But none of them helped.

Here are My PC Specs:

Upvotes: 10

Views: 4651

Answers (3)

Boney
Boney

Reputation: 2202

I'm using BrownPro font and the texts were blurry at all resolutions, but much more evident at low resolutions.

I was able to solve the issue by setting the hinting preference for the font to: PreferNoHinting. Applying it at the application level, fixes the issue everywhere.

Here is the documentation: https://doc.qt.io/qt-5/qfont.html#HintingPreference-enum

And here is the code I used:

QFontDatabase::addApplicationFont(":/fonts/BrownPro-Bold.ttf");
QFontDatabase::addApplicationFont(":/fonts/BrownPro-Regular.ttf");
QFontDatabase::addApplicationFont(":/fonts/BrownPro-Light.ttf");

QFont brown_pro_font("BrownPro");
brown_pro_font.setHintingPreference(QFont::HintingPreference::PreferNoHinting); //This line did the trick
QApplication::setFont(brown_pro_font);

Upvotes: 11

Arish Khan
Arish Khan

Reputation: 750

I happen to work with Qt last year and i used qml for building the UI part of my application.

Qt itself prefers us to use qml for building UI, since they have written a UI engine that renders everything better compared to the old engines.

In case of PyQt you are using the python only approach which is only not usually recommended, i am not saying that the qml version is pixel perfect. it still works bad at drawing curves (but that is not the stuff we usually require). As far as your problem is concerned qml will work fine for you (it has much better text rendering).

You might struggle a bit finding the learning resource for qml. But at least give it a shot and yes it is easier much easier than Python only approach.

Upvotes: 3

Try to see the fonts used by PyQt5:

import PyQt5
from pyQt5 import QtGui
dir(QtGui.QFont)

the result will show all you need for QFond and the fonts can be used:

[..., 'Helvetica',...,'SansSerif',..., 'Serif',..., 'Times', ...

You can try to add your custom fonts but you need to test each font.

For example, the documentation tells us:

In Windows a request for the “Courier” font is automatically changed to “Courier New”, an improved version of Courier that allows for smooth scaling. The older “Courier” bitmap font can be selected by setting the PreferBitmap style strategy (see setStyleStrategy() ). Once a font is found, the remaining attributes are matched in order of priority:

fixedPitch()

pointSize() (see below)

weight()

style()

Upvotes: 2

Related Questions