Results 1 to 6 of 6

Thread: Colored special characters in textEdit (<, >, &)

  1. #1
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Colored special characters in textEdit (<, >, &)

    Hello,

    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...
    Qt Code:
    1. QTextCursor cursor = textEdit->textCursor();
    2. 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>");
    To copy to clipboard, switch view to plain text mode 

    And here is the output (normally with color)...
    Qt Code:
    1. &ltAFK&gt
    To copy to clipboard, switch view to plain text mode 
    The problem is, instead of escaping the symbols, qt is displaying the text within the QString (such as "&lt").

    However...
    Qt Code:
    1. cursor.insertHtml("&lt")
    To copy to clipboard, switch view to plain text mode 
    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.

    Thanks,
    JvIasterMind

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Colored special characters in textEdit (<, >, &)

    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.

  3. #3
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored special characters in textEdit (<, >, &)

    Great!

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

  4. #4
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored special characters in textEdit (<, >, &)

    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...
    Qt Code:
    1. QMap<QChar, QString> colorCodes;
    2.  
    3. colorCodes['1'] = "#de0202";
    4. colorCodes['2'] = "#00ff00";
    5. colorCodes['3'] = "#f7f701";
    6. colorCodes['4'] = "#0405be";
    7. colorCodes['5'] = "#00ffff";
    8. colorCodes['6'] = "#ff00ff";
    9. colorCodes['7'] = "#ffffff";
    10. colorCodes['8'] = "#ff7f00";
    11. colorCodes['9'] = "#7f7f7f";
    12. colorCodes['0'] = "#000000";
    To copy to clipboard, switch view to plain text mode 

    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.

    JvIasterMind
    Last edited by JvIasterMind; 15th February 2010 at 00:35.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Colored special characters in textEdit (<, >, &)

    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 ;)

  6. The following user says thank you to Lykurg for this useful post:

    JvIasterMind (15th February 2010)

  7. #6
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Colored special characters in textEdit (<, >, &)

    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.


    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.


    Thanks again Lykurg!

Similar Threads

  1. Encode html special characters
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2009, 09:51
  2. special characters in xml
    By khcbabu in forum Qt Programming
    Replies: 3
    Last Post: 6th November 2008, 23:10
  3. QtXml and special characters
    By supergillis in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2008, 18:44
  4. QT 4 Xml parsing with special characters
    By KrishnaKishan in forum Qt Programming
    Replies: 1
    Last Post: 18th June 2007, 11:53
  5. Using special characters in QT dialogs
    By hvengel in forum Qt Tools
    Replies: 2
    Last Post: 10th April 2007, 01:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.