QPainter drawtext blurred text



  • When I draw text on either Qpixmap, QImage or QPrinter, the letters look blurred when I use drawtext with QPainter.

    Any ideas?



  • 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);
    

    // image.fill(0);
    image.fill(Qt::white);
    QPainter painter;
    painter.setRenderHints(QPainter::Antialiasing);
    painter.begin(&image);
    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);
    image.save("Texto.png");
    painter.restore();
    painter.end();

    painter.end();


  • @PabloArg

    Once try with (QPainter::TextAntialiasing)



  • I tried as well, same result



  • Also I tried font.setStyleStrategy(QFont::PreferAntialias); unsuccessfully as well



  • @PabloArg
    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();

  • Moderators

    @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:
    0_1486039313695_upload-6d303660-7657-49d8-90d4-9699ec719fce


  • Moderators

    @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.


  • Qt Champions 2016

    @PabloArg
    Hi i use
    www.postimage.org
    and then just paste link here.
    ( the Direct Link)




  • Qt Champions 2016

    Ahh
    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


  • Qt Champions 2016

    @PabloArg
    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
    QPrinter printer;
    QPainter painter(&printer);
    printer.setOutputFileName("c:\temp\file.pdf");
    printer.setOutputFormat(QPrinter::PdfFormat);
    painter.drawText(xxx)



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.