Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can one output '<' or '>' while setting the HTML of a QTextEdit?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 5.0k Views
  • 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 Offline
    J Offline
    javajames
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      goblincoding
      wrote on last edited by
      #2

      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
      0
      • J Offline
        J Offline
        javajames
        wrote on last edited by
        #3

        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
        0
        • G Offline
          G Offline
          goblincoding
          wrote on last edited by
          #4

          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
          0
          • J Offline
            J Offline
            javajames
            wrote on last edited by
            #5

            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
            0
            • G Offline
              G Offline
              goblincoding
              wrote on last edited by
              #6

              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
              0
              • B Offline
                B Offline
                b1gsnak3
                wrote on last edited by
                #7

                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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved