QPainter::DrawText antialiasing not work
Unsolved
General and Desktop
-
Env
Linux KDE
Qt 6.3.0Screenshot
Code
QPixmap pixmap(page_size); painter.begin(&pixmap); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); font.setStyleStrategy(QFont::PreferAntialias); font.setHintingPreference(QFont::PreferFullHinting); painter.setFont(font); painter.drawText(text_origin, qs_text); painter.end();
More Info
A similar post: https://forum.qt.io/topic/111322/high-dpi-support-qpainter-drawtext
It doens't solve the problem for me.Question
How to draw everything antialiasing in Qt6?
-
It could be related to the fact that you're drawing it on a
QPixMap
: https://bugreports.qt.io/browse/QTBUG-22334.Do you use DPI scaling in your desktop environment? If yes, with what setting?
-
Since you have taken a screenshot rather than inspected the actual QPixmap you are subject to whatever scaling happened putting the image on the screen. If that scaling up in size is not a nice ratio then you will see it. What is in the actual pixmap?
This code with Qt 6.2.3 on Linux:
#include <QGuiApplication> #include <QFont> #include <QPixmap> #include <QPainter> int main(int argc, char **argv) { QGuiApplication app(argc, argv); QFont font("Verdana"); QPoint text_origin(50, 20); QString qs_text("890,321,832.65"); QPixmap pixmap(800, 600); pixmap.fill(); QPainter painter; painter.begin(&pixmap); painter.setPen(Qt::green); painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); font.setStyleStrategy(QFont::PreferAntialias); font.setHintingPreference(QFont::PreferFullHinting); for (int size = 8; size < 40; size += 2) { font.setPointSize(size); painter.setFont(font); painter.drawText(text_origin, qs_text); QFontMetrics fm(font); painter.translate(0, fm.lineSpacing()); } painter.end(); pixmap.save("test.png"); return 0; }
produces this quite clean pixmap: