Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using Painter correctly in order to draw text.
QtWS25 Last Chance

Using Painter correctly in order to draw text.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ealioneE Offline
    ealioneE Offline
    ealione
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • E Offline
      E Offline
      euchkatzl
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • ealioneE Offline
        ealioneE Offline
        ealione
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          euchkatzl
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • ealioneE Offline
            ealioneE Offline
            ealione
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved