Text is not good enough while using QPainter::drawText()?



  • Hi.
    i'm trying to draw text using QPainter::drawText() but the text is not antialiased (comparing to MS word)

    void TextLabel::paintEvent(QPaintEvent*) {
        QPainter p(this);
        p.setRenderHint(QPainter::TextAntialiasing);
    
        QFont font;
        font.setFamily("Roboto medium");
        font.setPointSize(32);
        font.setStyleHint(QFont::Helvetica, QFont::PreferAntialias);
    
        p.setPen(_brush);
        p.setFont(font);
    
        p.drawText(rect(), Qt::AlignLeft , _text);
    }
    

    Qt Doc says:

    QPainter::TextAntialiasing -> Indicates that the engine should antialias text if possible

    Is this impossible ?
    What should i do ?

    The word one : https://drive.google.com/file/d/0B-nmbIyXfagPNlBaaGRyWGhXM2s/view
    The Qt one : https://drive.google.com/file/d/0B-nmbIyXfagPR3FiLXd1ZkptREk/view


  • Moderators

    @IMAN4K

    • which Qt version do you use?
    • did you compile Qt yourself?
    • which platform?

    i am not sure if it helps, but try to omit the Helvetica hint. And just set the stragedy:

    font.setStyleStrategy(QFont::PreferAntialias)
    

    But your text - in case you didn't downscale the posted images - is already anti-aliased. As you can see when you zoom into the image.
    I guess MS word and Qt are just using different anti-aliasing algorithms.



  • @raven-worx
    Using Qt5.5.1 on Windows.
    And no i didn't.

    That doesn't work too :(
    And yes they are not realy anti-aliased .


  • Moderators

    @IMAN4K said:

    And yes they are realy anti-aliased .

    Then i am afraid there is not much you can do. Since Qt doesn't offer an such detailed anti-aliasing API.



  • @raven-worx
    Ii means there is no way just for a simple text drawing ?


  • Moderators

    @IMAN4K
    as we already agreed anti-aliasing works.
    But not the way you would like it to. And the anti-aliasing algorithm can't be explicitly set AFAIK.



  • It seems Qt has issue on Window OS (font rendering) and work with some fonts >=48pt and doesn't work with some other.

    Issue : https://bugreports.qt.io/browse/QTBUG-40052

    We hope they will fix it in the near future.

    You can draw with QPainterPath it's more expensive but still helps :

    void TextLabel::paintEvent(QPaintEvent*) {
    	QPainter painter(this);
    	painter.setRenderHint(QPainter::Antialiasing);
    	painter.setBrush(Qt::black);
    
    	QFont font;
    	font.setPointSize(38);
    	font.setFamily("Roboto");
    
    	painter.setFont(font);
    
    	painter.drawText(0, 60, "Google");
    
    	QPainterPath textPath, path0;
    	textPath.addText(0, 140, font, "Google");
    	painter.drawPath(textPath);
    }
    

    Roboto @ 38pt :

    enter image description here


Log in to reply
 

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