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. QPainter how to draw text with different colors?
Qt 6.11 is out! See what's new in the release blog

QPainter how to draw text with different colors?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 24.6k 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.
  • K Offline
    K Offline
    Kaluss
    wrote on last edited by
    #1

    Hi,
    how could I draw text with different colors for e.g.

    "One Two" where One is yellow and Two is red ?

    BR,
    Tomek

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zaps166
      wrote on last edited by
      #2

      You can use this:

      QPainter painter(this);
      painter.setPen(Qt::yellow);
      painter.drawText(0, 20, "One");
      painter.setPen(Qt::red);
      painter.drawText(QFontMetrics(painter.font()).size(Qt::TextSingleLine, "One ").width(), 20, "Two");
      

      but maybe there are better solutions :)

      1 Reply Last reply
      1

      • Login

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