PDA

View Full Version : Change colour of QString



Morea
10th February 2006, 13:46
I need help with setting the colour of a string to be green in the beginning and red at the end.
The problem right now is that spaces that occur between the two colours disapear.
The code looks like this:



QString input = .. what ever the user inputs
QString answer = "foo bar";
QString temp="<font color=\"#00FF00\">";
int i=0;
while (i<input.size()&& i<answer.size() && input[i]==answer[i])
{
temp=temp+input[i];
i++;
}
temp=temp+"</font><font color=\"#FF0000\">"+answermid(i)+"</font>";

Cesar
10th February 2006, 14:25
The QString class provides a Unicode character string
QString isn't responsible for the way it is displayed. Is is just a convinient container for the characters with a bunch of useful features. The way QString is shown depends on the widget which displays it. In your case it is QTextEdit.


//The string you want to decorate
QString s = "adadsfdafds";
//The widget, which will display string
QTextEdit te;
//Format first 4 characters
QString redPart = QString("<span style=" color:#ff0000;">%1</span>").arg(s.left(4));
//Format the rest of the characters
QString greenPart = QString("<span style=" color:#00ff00;">%1</span>").arg(s.mid(5));
//Make resulting HTML appear on the QTextEdit
te.setHtml(redPart + greenPart);

Morea
10th February 2006, 14:30
Thanks for this answer Cesar, Will your code respect spaces in the text if they occur between the green and the red part?

Cesar
10th February 2006, 16:31
I need help with setting the colour of a string to be green in the beginning and red at the end.
The problem right now is that spaces that occur between the two colours disapear.
Try this code... And ask your questions, if any :)

GreyGeek
10th February 2006, 21:12
Try this code... And ask your questions, if any :)

It works for me if:
"what ever I enter in the second box, which matches character for character what is in the first box, from right to left, is turned green in the display form"
was what you intended.

Morea
10th February 2006, 21:22
But why doesn't the HTML code work with spaces?

wysota
10th February 2006, 22:07
Because HTML trimms multiple whitespaces into one. If you want to keep them, transform each of the multiple spaces to "&nbsp;" (keep single ones as they are).

Morea
10th February 2006, 22:27
Thanks for all the help so far, but now I'm stuck. I get a crash when the program starts.
The problem seems to be with the creation of the red/green part.
The point of the program is to press enter, get a random text, and directly start writing the same thing (marked red/green if wrong/right).

I attach a tgz with the code.
It's produced with dev-c++ in windows.

Morea
10th February 2006, 22:36
Ah, is it perhaps the infinite loop I get with all the textChanged signals that are emitted, and the KollaText() slot that receives it and then again emits a textChanged signal..
It might be! But I doesn't seem possible to change to a keypressed event or something like that. Any ideas, or should I just use the code that cesar sent earlier?

Morea
10th February 2006, 23:31
Now it works fine! I strongly recomend anyone using Dev-C++ to rebuild everything once in a while, otherwise you might have parts that is not properly updated.