Why does QPainter::TextAntialiasing doesn't work in my code?
-
Hi there,
on Windows 10 desktop I have the feeling that text antialiasing does not work in my Qt 5.7.0 code. I can not see any difference between the following first two Qt based screenshots. But in the third one, done with Visual C#, antialiasing works:
Load them in different browser tabs and switch between them:
https://s19.postimg.org/4321qkavn/Qt_5_7_0_no_antialiasing.png
https://s19.postimg.org/rijyvwum/Qt_5_7_0_with_antialiasing.png
https://s19.postimg.org/4or0mo5xv/Visual_C.pngLook for example at the letters 'S' or 'a'. The last Visual C# based screenshot look a lot smoother, whereas both Qt screenshots look the same pixelated.
Please look at my code. Why text antialiasing doesn't work? What's wrong in my code?
Is there anything else beside the code to consider on Windows to make text antialiasing work?
#include <QApplication> #include <QWidget> #include <QPainter> class Widget : public QWidget { public: Widget(); protected: void paintEvent(QPaintEvent *); }; Widget::Widget() { setAutoFillBackground(true); setPalette(Qt::white); } void Widget::paintEvent(QPaintEvent *) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::TextAntialiasing, true); painter.setRenderHint(QPainter::SmoothPixmapTransform, true); QFont Roboto("Roboto", 24, QFont::Normal); QFont SourceSansPro("Source Sans Pro", 24, QFont::Normal); QFont ClearSans("Clear Sans", 24, QFont::Normal); QFont Ubuntu("Ubuntu", 24, QFont::Normal); QFont SegoeUI("SegoeUI", 24, QFont::Normal); painter.setFont(Roboto); painter.drawText(QPointF(20.0, 60.0), "Qt 5.7.0 on Windows Desktop: with anti aliasing:"); painter.drawText(QPointF(20.0, 140.0), "The quick brown fox jumps over the lazy dog: Roboto"); painter.setFont(SourceSansPro); painter.drawText(QPointF(20.0, 220.0), "The quick brown fox jumps over the lazy dog: Source Sans Pro"); painter.setFont(ClearSans); painter.drawText(QPointF(20.0, 300.0), "The quick brown fox jumps over the lazy dog: Clear Sans"); painter.setFont(Ubuntu); painter.drawText(QPointF(20.0, 380.0), "The quick brown fox jumps over the lazy dog: Ubuntu"); painter.setFont(SegoeUI); painter.drawText(QPointF(20.0, 460.0), "The quick brown fox jumps over the lazy dog: SegoeUI"); } int main(int argc, char *argv[]) { QApplication app(argc, argv); Widget widget; widget.showMaximized(); return app.exec(); }
-
antialiasing != antialiasing
Means there are different strategies do achieve antialiasing and the result may differ. -
And your second link is broken ... it says the image was unavailable/removed.
-
As per my understanding by default QPainter paints with anti-aliasing feature.
Any way can you try this..?QFont Roboto("Roboto", 24, QFont::Normal);
Roboto.setStyleStrategy(QFont::PreferAntialias);
painter.setFont(Roboto);
painter.drawText(QPointF(20.0, 60.0), "Qt 5.7.0 on Windows Desktop: with anti aliasing:");