How to set different background-color for part of text in QLabel
-
wrote on 28 Apr 2017, 08:37 last edited by
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.
-
wrote on 28 Apr 2017, 08:51 last edited by 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)**");
-
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.
wrote on 28 Apr 2017, 08:54 last edited by Ni.SumiAFAIK, One you can use linear gradient on the QLabel. If you don't need exact end for colors.
- Easy way, Use some color band image like this as background .It is same as you want .
- Easy way, Use some color band image like this as background .It is same as you want .
-
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)**");
wrote on 28 Apr 2017, 09:54 last edited by@VRonin
Thanks, it works!
But What is R"** in front of text? -
@VRonin
Thanks, it works!
But What is R"** in front of text?wrote on 28 Apr 2017, 09:56 last edited by@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");
3/5