Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Different combinations of ClearType text but It didn't work out as, by default all the text appears good on windows and chrome but with Qt only, it becomes pixelated.

  • Changing windows aero theme to classic one...

But none of them helped.

Here are My PC Specs:

  • windows: 7 ultimate
  • PySide2 version: 5.14.2.1
  • Resolution: 1360 X 768
like image 938
Abhay Salvi Avatar asked May 09 '20 14:05

Abhay Salvi


People also ask

Why are my fonts pixelated?

Anti-Aliasing is the most common reason why text can appear pixelated, or it could even be the font itself. If the resolution of the image or project you are working on is too low, you may also experience pixilation of your text.

How do I fix pixelated fonts in Windows?

On the Start Menu, type Clear Type and open the option "Adjust ClearType text" follow the instructions until the end.

How do I change the font in Qt?

Simply use the setFont() method on the QApplication or QWidget : QFont font("Courier New"); font. setStyleHint(QFont::Monospace); QApplication::setFont(font);


Video Answer


2 Answers

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);
like image 68
Boney Avatar answered Oct 09 '22 20:10

Boney


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()
like image 21
Cătălin George Feștilă Avatar answered Oct 09 '22 19:10

Cătălin George Feștilă