Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How do I change the color of a label text?

    General and Desktop
    4
    7
    73796
    Loading More Posts
    • 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
      buon43 last edited by

      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 Reply Quote 1
      • C
        clochydd last edited by

        Hi,
        you can do that with styleSheet:

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

        1 Reply Last reply Reply Quote 0
        • X
          Xander84 last edited by

          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 Reply Quote 0
          • B
            buon43 last edited by

            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 Reply Quote 0
            • Chris Kawa
              Chris Kawa Moderators last edited by

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

              1 Reply Last reply Reply Quote 0
              • X
                Xander84 last edited by

                @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 Reply Quote 3
                • B
                  buon43 last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post