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. Easiest way to paint one letter of the text different color with QPainter::drawText?
Forum Updated to NodeBB v4.3 + New Features

Easiest way to paint one letter of the text different color with QPainter::drawText?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 2.5k Views 2 Watching
  • 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.
  • V Offline
    V Offline
    Violet Giraffe
    wrote on last edited by Violet Giraffe
    #1

    I know how to drawText (at least the basics), and I know you can change text color with QPainter::setPen. What I need to do is a bit trickier, though: I need one character somewhere in the middle of the string to be painted different (non-default) color.

    I suppose I could:

    1. Use QString::left to extract whatever first characters are default-colored and draw them.
    2. Call setPen with the color for the differently-colored character.
    3. Calculate the rect for the first part of the string using QFontMetrics::boundingRect.
    4. Draw the odd character using bounding rect from 4. as offset.
    5. Add the offset for the odd letter the same way.
    6. setPen back to default and draw the rest of the string using offset calculated at 5.

    It looks a bit shaky, though. For one, I have my doubts that the rects will add up perfectly and that the result will be exactly the same as if I wasn't splitting the text and painted it with a single drawText call.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! How about the following:

      void MyWidget::paintEvent(QPaintEvent*)
      {
          QPainter painter(this);
          QTextDocument doc;
          doc.setHtml("<font color=\"#f00\">H</font>allo");
          doc.drawContents(&painter);
      }
      
      1 Reply Last reply
      4
      • V Offline
        V Offline
        Violet Giraffe
        wrote on last edited by Violet Giraffe
        #3

        I see what you did there! One problem, though (should have mentioned it in the question): the reason I have to use QPainter in the first place (instead of e. g. QLabel) is I need to position the text in a certain way (offset it) inside the widget. Is that doable with QTextDocument::drawContents?

        ? 1 Reply Last reply
        0
        • V Violet Giraffe

          I see what you did there! One problem, though (should have mentioned it in the question): the reason I have to use QPainter in the first place (instead of e. g. QLabel) is I need to position the text in a certain way (offset it) inside the widget. Is that doable with QTextDocument::drawContents?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @Violet-Giraffe Sure, just apply transformations to the painter as you wish.

          1 Reply Last reply
          2
          • V Offline
            V Offline
            Violet Giraffe
            wrote on last edited by
            #5

            Oh, you can translate the painter after drawing to it! Neat!

            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