Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QPainter drawtext blurred text
When I draw text on either Qpixmap, QImage or QPrinter, the letters look blurred when I use drawtext with QPainter.
Venkatesh V last edited by
Hi @PabloArg ...
use setRenderHints(QPainter::Antialiasing) with Qpainter.
Hi Venkatesh, many thanks for your quick reply, but I tried with setRenderHints using the option that you mention as well I used TextAntialiing and I got same results.
I using Qt creator qt-opensource-windows-x86-mingw530-5.8.0.
QImage image(ancho,alto, QImage::Format_ARGB32);
painter.setPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::RoundCap));
QRectF r1(0, 0, ancho-1,alto-1);
Venkatesh V last edited by
Once try with (QPainter::TextAntialiasing)
I tried as well, same result
Also I tried font.setStyleStrategy(QFont::PreferAntialias); unsuccessfully as well
Eeli K last edited by
I'm not sure if I have understood the code snippets correctly, but in any case antialiasing makes the text (or any edge) blurred, that's its purpose, to smoothen out sharp edges. It should be turned off if you want sharp pixels.
Hi Eeli K, Well when you run the code Sharp edges are because setbold is false.
When setbold is true it looks blurry.
I will post my code again, when you get the picture open with GIMP or Other and make zoom up to 300 and will be very clear the problem.
QString Texto = QDateTime::currentDateTime().toString(); QFont font; font.setBold(true); font.setFamily("Helvetica"); font.setStyleStrategy(QFont::PreferAntialias); font.setPixelSize(12); font.setStyleHint(QFont::SansSerif,QFont::PreferOutline); QFontMetrics fm(font); qreal ancho = fm.width(Texto); qreal alto = fm.height(); QImage image(ancho,alto, QImage::Format_ARGB32); // image.fill(0); image.fill(Qt::white); QPainter painter; painter.begin(&image); painter.setRenderHints(QPainter::TextAntialiasing,true); painter.setRenderHints(QPainter::Antialiasing,true); painter.save(); // painter.setCompositionMode(QPainter::CompositionMode_SourceOver); painter.setFont(font); painter.setPen(QPen(Qt::black, 0, Qt::SolidLine, Qt::RoundCap)); QRectF r1(0, 0, ancho-1,alto-1); // painter.drawRect(r1); painter.drawText(r1,Qt::AlignCenter,Texto); // painter.drawText(10,10,100,100,Qt::AlignCenter,Texto); image.save("Texto.png"); painter.restore(); painter.end();
@PabloArg Nothing is blurred on my Windows 7 machine with your code. What I see is what antialising is doing.
The problem is, that I generate a PDF and when I print it looks bad.
Anyway I got this:
@PabloArg Image sharing is usually not working here. Please upload the picture to image sharing service and post the link here.
Hi jsulm, I cannot find the link that you suggest.
Its when inside the PDF ?
Since you print it to image and then print image to pdf, its not
a vector font anymore.
its simply a image of a font/text and will get ugly when zoomed.
Mrjj you say that the Helvetic font is not vectorial, so if you know could you tell me some font that is vectorial on windows I will appreciate
No, not like that.
It is vector but you make it part of an image and i think
what you mean is that is not scaling nicely as other text in pdf normally do ?
The reason for that is its no longer text, just pixels on image.
Try drawing the text directly on the Printer painter
mrjj. Make sense that you say. I saw that if I draw directly using qpdfwriter the text looks good.
So, I will follow your advise, write directly on QPrinter or qpdfwriter.
Many thanks everybody