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. Setting the text of QLabel to the color of a Pixmap
Qt 6.11 is out! See what's new in the release blog

Setting the text of QLabel to the color of a Pixmap

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.1k Views 1 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.
  • M Offline
    M Offline
    MichaelStein
    wrote on last edited by
    #1

    Hi everyone,

    I have been struggling to set the text of my QLabel to the color and texture of a Pixmap. I want the text in the label to have a gold color with a shiny tinge. So I loaded a Pixmap image into the resources and created a Painter event on the QLabel. In the paint event I entered the following code:

    @
    void labelPaint::paintEvent(QPaintEvent *)
    {
    QBrush brush;

    QPixmap Pixmap(":/gold.jpg");
    
    brush.setTexture(Pixmap);
    
    brush.setPixmap(Pixmap);
    
    QPainter painter(this);
    
    painter.setFont(QFont("Times New Roman", 30));
    
    painter.setBrush(brush);
    
    QString myString("some string");
    
    painter.drawText(510,40,myString);
    

    }@

    And I still get the same plain black color as my text. Note that I used both setTexture and setPixmap in my atttempt.
    Thank in advance

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      For drawing lines, outlines and text QPainter uses a pen. Brush is for filling shapes and paths. So instead of changing the brush of the painter change the brush of the pen of the painter (yeah, I know :) ).

      @
      QBrush brush;
      ...//setup brush
      painter.setPen(QPen(brush, 1.0));
      @

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MichaelStein
        wrote on last edited by
        #3

        Great, thank you it worked!

        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