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.5k 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.
  • kahlenbergK Offline
    kahlenbergK Offline
    kahlenberg
    wrote on 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.

    Ni.SumiN 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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

      kahlenbergK 1 Reply Last reply
      4
      • kahlenbergK kahlenberg

        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.

        Ni.SumiN Offline
        Ni.SumiN Offline
        Ni.Sumi
        wrote on 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
        • VRoninV VRonin

          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)**");

          kahlenbergK Offline
          kahlenbergK Offline
          kahlenberg
          wrote on last edited by
          #4

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

          VRoninV 1 Reply Last reply
          0
          • kahlenbergK kahlenberg

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

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on 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

            • Login

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