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 to set different background-color for part of text in QLabel
Forum Updated to NodeBB v4.3 + New Features

How to set different background-color for part of text in QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 7.6k 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.
  • K Offline
    K Offline
    kahlenberg
    wrote on 28 Apr 2017, 08:37 last edited by
    #1

    Hi,
    I want to set background-color of a part of text in QLabel differently. I can use QLabel.setStyleSheet method but it changes whole background. I only want to change background of particular text.
    For example:
    Text to be written in QLabel: 00-01-02-03-04-05
    I want to have all text black, backgorund-color of "00" shall be red, background-color of "01" shall be blue and so on.
    How can I do that?

    Thanks.

    N 1 Reply Last reply 28 Apr 2017, 08:54
    0
    • V Offline
      V Offline
      VRonin
      wrote on 28 Apr 2017, 08:51 last edited by VRonin
      #2

      http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf page 11

      See also: http://doc.qt.io/qt-5/richtext-html-subset.html

      label->setText(R"**(<span style="background-color:red;">00</span>-<span style="background-color:blue;">01</span>-02-03-04-05)**");

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      K 1 Reply Last reply 28 Apr 2017, 09:54
      4
      • K kahlenberg
        28 Apr 2017, 08:37

        Hi,
        I want to set background-color of a part of text in QLabel differently. I can use QLabel.setStyleSheet method but it changes whole background. I only want to change background of particular text.
        For example:
        Text to be written in QLabel: 00-01-02-03-04-05
        I want to have all text black, backgorund-color of "00" shall be red, background-color of "01" shall be blue and so on.
        How can I do that?

        Thanks.

        N Offline
        N Offline
        Ni.Sumi
        wrote on 28 Apr 2017, 08:54 last edited by Ni.Sumi
        #3

        @kahlenberg

        AFAIK, One you can use linear gradient on the QLabel. If you don't need exact end for colors.

        1. Easy way, Use some color band image like this as background .It is same as you want .
          alt text

        0_1493370474679_images.jpg

        1 Reply Last reply
        1
        • V VRonin
          28 Apr 2017, 08:51

          http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf page 11

          See also: http://doc.qt.io/qt-5/richtext-html-subset.html

          label->setText(R"**(<span style="background-color:red;">00</span>-<span style="background-color:blue;">01</span>-02-03-04-05)**");

          K Offline
          K Offline
          kahlenberg
          wrote on 28 Apr 2017, 09:54 last edited by
          #4

          @VRonin
          Thanks, it works!
          But What is R"** in front of text?

          V 1 Reply Last reply 28 Apr 2017, 09:56
          0
          • K kahlenberg
            28 Apr 2017, 09:54

            @VRonin
            Thanks, it works!
            But What is R"** in front of text?

            V Offline
            V Offline
            VRonin
            wrote on 28 Apr 2017, 09:56 last edited by
            #5

            @kahlenberg said in How to set different background-color for part of text in QLabel:

            What is R"** in front of text?

            It's a raw string literal I use it to avoid escaping quotes inside span, otherwise you should write:
            label->setText("<span style=\"background-color:red;\">00</span>-<span style=\"background-color:blue;\">01</span>-02-03-04-05");

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            4

            3/5

            28 Apr 2017, 08:54

            • Login

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