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. Change Label text color not working

Change Label text color not working

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 4.5k 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.
  • O Offline
    O Offline
    o6a6r9v1p
    wrote on 9 Feb 2017, 09:24 last edited by
    #1

    I have gone through Qt forums and used different examples given for changing the color of Label text, unsuccessfully.

    The following is One snippet found in one Qt forum, but giving error.

    @
    QColor colour = "red";
    QString text;
    text = "abcd";
    QString format("<font color="%1">%2</font>");
    Labelname->setText(format.arg(colour.name(), text));
    @

    Error is "Invalid operand types " in
    QString format("<font color="%1">%2</font>");
    program line.

    another question is : is it possible to change background color of Label.

    J J 2 Replies Last reply 9 Feb 2017, 09:31
    0
    • O o6a6r9v1p
      9 Feb 2017, 09:24

      I have gone through Qt forums and used different examples given for changing the color of Label text, unsuccessfully.

      The following is One snippet found in one Qt forum, but giving error.

      @
      QColor colour = "red";
      QString text;
      text = "abcd";
      QString format("<font color="%1">%2</font>");
      Labelname->setText(format.arg(colour.name(), text));
      @

      Error is "Invalid operand types " in
      QString format("<font color="%1">%2</font>");
      program line.

      another question is : is it possible to change background color of Label.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 9 Feb 2017, 09:31 last edited by
      #2

      @o6a6r9v1p said in Change Label text color not working:

      QString format("<font color="%1">%2</font>");

      You need to escape ":

      QString format("<font color=\"%1\">%2</font>");
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply 9 Feb 2017, 09:40
      1
      • O o6a6r9v1p
        9 Feb 2017, 09:24

        I have gone through Qt forums and used different examples given for changing the color of Label text, unsuccessfully.

        The following is One snippet found in one Qt forum, but giving error.

        @
        QColor colour = "red";
        QString text;
        text = "abcd";
        QString format("<font color="%1">%2</font>");
        Labelname->setText(format.arg(colour.name(), text));
        @

        Error is "Invalid operand types " in
        QString format("<font color="%1">%2</font>");
        program line.

        another question is : is it possible to change background color of Label.

        J Offline
        J Offline
        J.Hilk
        Moderators
        wrote on 9 Feb 2017, 09:31 last edited by
        #3

        @o6a6r9v1p

        you should look into QStyleSheet

        for example this makes it a blue background with red letters:

        ui->label->setStyleSheet("QLabel {background-color: blue; color: red;}");
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        O 1 Reply Last reply 9 Feb 2017, 09:44
        1
        • J jsulm
          9 Feb 2017, 09:31

          @o6a6r9v1p said in Change Label text color not working:

          QString format("<font color="%1">%2</font>");

          You need to escape ":

          QString format("<font color=\"%1\">%2</font>");
          
          O Offline
          O Offline
          o6a6r9v1p
          wrote on 9 Feb 2017, 09:40 last edited by
          #4

          @jsulm
          I did, But no change in text color, it is black only

          J 1 Reply Last reply 9 Feb 2017, 09:44
          0
          • O o6a6r9v1p
            9 Feb 2017, 09:40

            @jsulm
            I did, But no change in text color, it is black only

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 9 Feb 2017, 09:44 last edited by
            #5

            @o6a6r9v1p Works for me

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • J J.Hilk
              9 Feb 2017, 09:31

              @o6a6r9v1p

              you should look into QStyleSheet

              for example this makes it a blue background with red letters:

              ui->label->setStyleSheet("QLabel {background-color: blue; color: red;}");
              
              O Offline
              O Offline
              o6a6r9v1p
              wrote on 9 Feb 2017, 09:44 last edited by
              #6

              @J.Hilk
              what is default background color in Windows OS. I mean if we want to revert back to default color, what shall we do?

              J 1 Reply Last reply 9 Feb 2017, 09:51
              0
              • O o6a6r9v1p
                9 Feb 2017, 09:44

                @J.Hilk
                what is default background color in Windows OS. I mean if we want to revert back to default color, what shall we do?

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 9 Feb 2017, 09:51 last edited by
                #7

                @o6a6r9v1p

                iirc its white,

                but you can also set a new StyelSheet where you leave the color unspecified

                style1: "background-color: blue; color: red;";
                style2: "background-color: blue; ";
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                O 1 Reply Last reply 10 Feb 2017, 03:48
                1
                • J J.Hilk
                  9 Feb 2017, 09:51

                  @o6a6r9v1p

                  iirc its white,

                  but you can also set a new StyelSheet where you leave the color unspecified

                  style1: "background-color: blue; color: red;";
                  style2: "background-color: blue; ";
                  
                  O Offline
                  O Offline
                  o6a6r9v1p
                  wrote on 10 Feb 2017, 03:48 last edited by
                  #8

                  @J.Hilk
                  Got it working with your code.
                  jsulm's method didnot work in my code.
                  Thanks to both of you.

                  1 Reply Last reply
                  0

                  4/8

                  9 Feb 2017, 09:40

                  • Login

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