How can one output '<' or '>' while setting the HTML of a QTextEdit?



  • I'm using a QTextBrowser class for displaying some text. The problem is I'm outputting using HTML (QTextEdit::insertHtml(QString) specifically) and I need to be able to output '<' and '>'. I tried replacing these characters with "&lt" and "&gt" respectively but they don't display properly (they don't display as the respective characters, instead it just outputs "&lt".) Is there anyway to display the characters without affecting the parsing of HTML?

    (I need to be able to do this for user input; the user inputs a message and that string needs to be sanitized to prevent abuse.)

    Thanks in advance.



  • Do you mean you want to insert HTML inside HTML (i.e. have an HTML document displaying HTML text)? Or do you simply want to display HTML text in the text edit?

    For the first scenario, have you tried escaping the special characters, e.g. <blahblah \ /> ?

    If the second option is what you're after, try using setPlainText(QString).

    Hope that helps :)



  • Escaping characters is an input thing for text managed to machine managed.. Since you're passing text to the HTML parser escaping it does absolutely nothing ('<' == '<'.) Having said that I tried putting a slash preceding the characters when passing to the HTML parser ("\<") which did what expected - nothing (since nothing claims this happens in the documentation.)

    I can't format with HTML if I use setPlainText... I specifically need to be able to process the HTML as one so don't suggest looping insertHtml followed by insertTexts (this would also be slow.)

    Thanks for your attempt though :)



  • Oh, I see...sorry I couldn't be more useful! Hopefully someone more knowledgeable comes around soon.



  • Got it.

    The problem was I was writing "&lt" where the HTML parser expects the syntax applying to "&lt;"

    Also, the function Qt::escape() can be used to fully escape HTML.

    (http://stackoverflow.com/questions/7696159/how-can-i-convert-entity-characterescape-character-to-html-in-qt/15202392#15202392)



  • Excellent! It would be greatly appreciated if you could mark the thread as "Solved" (you can edit the topic in your first post itself).

    Thanks for posting the solution :)



  • You have more then just < and > to worry about... You also have & and " which can be set with &amp; and &ampquot;


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.