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. How do I change the color of a label text?
QtWS25 Last Chance

How do I change the color of a label text?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 95.8k 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.
  • B Offline
    B Offline
    buon43
    wrote on last edited by
    #1

    One way is to do it like this:

    ui->label->setText("<font color='red'>text</font>");

    But I want the user to be able to set the color. I have the color in QColor color, but how do I change the label's text color to that? Doesn't obviously work like this:

    QColor color = "orange";
    ui->label->setText("<font color='color'>text</font>");

    So how can I do it?

    1 Reply Last reply
    1
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      Hi,
      you can do that with styleSheet:

      @
      ui->label->setStyleSheet("{color: #C0BBFE}");
      @

      1 Reply Last reply
      1
      • X Offline
        X Offline
        Xander84
        wrote on last edited by
        #3

        Hi, you can change the color palette of the QLabel and set the WindowText (that will change the text color).
        in the UI editor click on "Change Palette" and play around with that, if you need to do that in c++ you can change the color in the UI file and copy the generated c++ code from the *_ui.h file (or do it yourself). If you want to reuse your QColor and not some CSS styles.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          buon43
          wrote on last edited by
          #4

          That doesn't exactly solve the problem. The QColor color = "orange" was just thrown there as an example. What I have is closer to something like this:

          QColor color = QColorDialog::getColor(Qt::white, this);

          So how do I set the color of the font to match that color?

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

            Yet another way:
            @QColor color = ...;
            QString format("<font color="%1">%2</font>");
            label->setText(format.arg(color.name(), text));
            @

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on last edited by
              #6

              @buon43: you can use the style sheet like Clochydd said or change the palette color like I said, so what is your problem with that? Both ways are not related to the text in the label so you can change the label color and text independently.

              maybe you need an example, I told you how you can see the c++ code and generate it yourself, but well here you go:
              @
              QColor color = QColorDialog::getColor(Qt::white, this);
              QPalette palette = ui->label->palette();
              palette.setColor(QPalette::WindowText, color);
              ui->label->setPalette(palette);
              @
              maybe that's a little bit more code but you can directly use your QColor.

              1 Reply Last reply
              3
              • B Offline
                B Offline
                buon43
                wrote on last edited by
                #7

                Thanks, that worked. Certainly more work than I thought for setting up a color like that.

                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