Using Painter correctly in order to draw text.



  • Hi everyone,

    I was learning how to draw various things in Qt using painter when I arrived at the subject of text.

    What I did was something like this:

    @painter.save();

    QRect rec(0, 0, 20, 30);
    painter.setPen(Qt::black);
    //painter.drawText(rec, Qt::AlignLeft, "am");

    QTextDocument doc;
    doc.setTextWidth(rec.width());

    doc.setHtml("<b>am</b>");
    doc.drawContents(&painter, rec);

    painter.restore();@

    So as you can see I tried using "drawText" and QTextDocument.

    I noticed though that in my Mac the resulting seems better (by that I mean more crisp or antialised), while trying it on Windows the text seems more pixelated even though I set every flag I know

    @painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHints(QPainter::HighQualityAntialiasing);
    painter.setRenderHints(QPainter::SmoothPixmapTransform);
    painter.setRenderHints(QPainter::TextAntialiasing);@

    Why is this happening?

    Also I could not find a way to alter the size of the text. Can someone tell me (or give me a link) a simple template on drawing and customising text using Painter?



  • I think this is related to several problems on windows.

    https://bugreports.qt.io/browse/QTBUG-44437
    --> QGraphicsTextItem is using an QTextDocument internal

    https://bugreports.qt.io/browse/QTBUG-20900

    Is there any reason you using QTextDocument.

    For simple Text you could for example draw it like :
    @QFont font("Arial",12);
    font.setBold(true);
    font.setItalic(true);
    painter.setFont(font);

    QTextOption opt;
    opt.setAlignment(Qt::AlignCenter);

    painter.drawText(QRectF(0,0,50,50),"This is yout text",opt);@



  • I see, thanks for the help and the link. I will admit its kind of annoying to have the text look like that on windows, in any case what can you do.

    Cheers.



  • When you want the same look on windows and mac you can try to use setPixelSize on QFont :
    http://doc-snapshot.qt-project.org/qt5-5.4/qfont.html#setPixelSize

    That will ensure that the font has the same size on different platforms.

    When nothing helps you could also try to generate a QPainterPath.



  • I will focus making the app look as I want it on unix, and the try to fix all the issues that may come up on windows. I did implement your suggestion, seems fine till now.


Log in to reply
 

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