View Full Version : Colored special characters in textEdit (<, >, &)

14th February 2010, 23:07

I am working on a simple program that needs to display colored text in a QTextEdit. I am using html and a css style within <span></span> tags to set the colors of the text. It works fine, except I can not get the text to display the less than symbol (<) with color. The greater than (>) and ampersand (&) have the same problem as < when using their escape codes (&gt and &amp) but seem to display if I do not use the escape codes.

Here is what I am trying to get working...

QTextCursor cursor = textEdit->textCursor();
cursor.insertHtml("<span style=\"color:#de0202\">&lt</span><span style=\"color:#000000\">A</span><span style=\"color:#0405be\">F</span><span style=\"color:#000000\">K</span><span style=\"color:#de0202\">&gt</span>");

And here is the output (normally with color)...
The problem is, instead of escaping the symbols, qt is displaying the text within the QString (such as "&lt").

properly displays "<" (without color). I just can't get it to display the escaped characters when it is within the span tags.

Note: When I put this same line of html in a web page, it displayed properly with the escape codes being replaced by the appropriate characters.

Any help or suggestions would be greatly appreciated.


14th February 2010, 23:16
I haven't looked through your code because there is a much better solution for you! Have a look at the Syntax Highlighter Example in the docs. Use that and you don't have to add html. Just use the native color/format option of the text edit.

14th February 2010, 23:23

Thanks for the quick reply. I am looking into it! :)

15th February 2010, 00:22
OK that example is a bit confusing to me. (I am still slightly new to qt)

What I have is a QMap that contains color codes for certain characters. I am starting with the numbers 1-9, but more will be added after I figure this out.

Here is the QMap...

QMap<QChar, QString> colorCodes;

colorCodes['1'] = "#de0202";
colorCodes['2'] = "#00ff00";
colorCodes['3'] = "#f7f701";
colorCodes['4'] = "#0405be";
colorCodes['5'] = "#00ffff";
colorCodes['6'] = "#ff00ff";
colorCodes['7'] = "#ffffff";
colorCodes['8'] = "#ff7f00";
colorCodes['9'] = "#7f7f7f";
colorCodes['0'] = "#000000";

I am dealing with text that has this format... ^1<^0A^4F^0K^1> T^2est, where ^ followed by a character makes the following text the corresponding color until it encounters the next ^ or the end of the line. I need to remove the ^[c] parts and instead make the text the proper colors for display.

Currently I split the string using ^ as a separator and get the color based on the first character in each substring and apply the color to the remaining characters in each substring. With this new method, it appears I no longer would need to do this.

It seems the example that they apply the colors after all the text is inputted into the textEdit using a series of patterns that detect where the color needs to be changed. I am having a hard time understanding how they are coming up with those pattern rules (such as QRegExp("\\bQ[A-Za-z]+\\b") and QRegExp("\".*\"").

I think I would need to start with ^\^ to detect the '^' symbol, but I am unsure how to continue.
<Edit>Hmmm... maybe I would rather start with '\^'. But then I need to allow any number of other characters and finally end with \^ or \b?

Again any help will be greatly appreciated.


15th February 2010, 20:48
Ok, that seems to be a different situation. With the syntax highlighting it is only possible to give all characters the same colour. (It is also possible to alter the colour but therefore you need complex regular expressions). I don't know how your colour informations behave and if the alter. So you want to stay with your solution.

The error in your first post was a missing ";". It has to be: "&lt;". Then it will be displayed as <.

(that inserting "&lt" will end in < if you put it in single is a comfort Qt provides you. In HTML it is not the right syntax. Therefore every character encoding begins with & and ends with ;)

15th February 2010, 21:37
Oh wow... thank you very much! Worked perfectly!

It is Firefox that threw me off (allowing me to insert the text improperly). It is amazing how missing something so small can give you hours of frustrations. :p

BTW, I am so glad that I discovered this forum. I guess this problem lead me to these forums, so in way it was worth it. :D

Thanks again Lykurg!