How to set different background-color for part of text in QLabel
-
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.
-
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.
-
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)**");
@VRonin
Thanks, it works!
But What is R"** in front of text? -
@VRonin
Thanks, it works!
But What is R"** in front of text?@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");