2

I am trying to find out if there is a way to split the text contained inside a QLabel into multiple labels, one for each character in the text.

What I am trying to accomplish is to be able to color each character in the label individually, so I can make them either red or green. I am trying to build a touch typing game where the user inputs data and it gets checked against a reference string he is looking at, which gets red or green depending on if he made a mistake or not.

ypnos
  • 50,202
  • 14
  • 95
  • 141
Adrian Costin
  • 418
  • 3
  • 12

1 Answers1

3

Use Qt's rich text facilities to color your characters. It is a subset of HTML (docs). Example:

label.setText("A<span style='color: red;'>B</span>C");

You can use QLabel::setTextFormat(Qt::TextFormat) to enable or disable this functionality. The default is enabled-by-heuristic (Qt::AutoText).

ypnos
  • 50,202
  • 14
  • 95
  • 141