Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    7
    4431
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      javajames last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • G
        goblincoding last edited by

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

        http://www.goblincoding.com

        1 Reply Last reply Reply Quote 0
        • J
          javajames last edited by

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

          1 Reply Last reply Reply Quote 0
          • G
            goblincoding last edited by

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

            http://www.goblincoding.com

            1 Reply Last reply Reply Quote 0
            • J
              javajames last edited by

              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)

              1 Reply Last reply Reply Quote 0
              • G
                goblincoding last edited by

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

                http://www.goblincoding.com

                1 Reply Last reply Reply Quote 0
                • B
                  b1gsnak3 last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post