Important: Please read the Qt Code of Conduct -

[SOLVED]Saving html tags in QString and displaying them correctly in a QTextBrowser

  • Hello,

    Can anyone tell me how do I save html tags in a QString from a QTextBrowser? Afterwards I will need to put it back in QTextBrowser with the appropiate tags. Getting the text with toPlainText() doesn't work. Tried with toHtml() and then when putting the qstring back with setHtml() but it doesn't work. I'm using this to customize the text displayed(bold, italic, underline) and I need to have an option that adds and removes timestamps from the text. To add / remove the timestamps I have to go through all the text in the textbrowser. It displays something like:

    UserName: text (without timestamps)
    UserName (timestamp): text (with timestamps)

    BTW doesn't work with "&lt" and "&gt". It saves the tags but displays the tags in textbrowser (don't need to display them, just to make them work)

    Thank you in advance.

    Qt 4.8.4 with MinGW
    Windows 7

  • QTextBrowser only supports a "subset of the HTML standard": Calling toHtml(), it's going to do it's best to convert your document into the HTML it supports. If that document didn't originate from a call to setHtml(), there is no promise that the layout is going to be spot on. Actually, you can pretty much bet that it won't be.

    But you can do all this through the QTextDocument directly. You can insert new text fragments or edit existing fragments (for bolding/underlining/etc) with QTextCursor.

  • @Jason.Dolan I know this. I used this already when first entered the html tags in the QTextBrowser. The only problem is that I need to rework the TextBrowser text so that I can insert some strings into it. For example, in yahoo, when you enable or disable the timestamps. I want to add the timestamps in between the username and text portions of text.

    Anyway... Managed it by adding a QStringList for every line in which I saved the strings with tags, and then added them to the TextBrowser.

    Don't know if it's the best idea though :)

Log in to reply