PDA

View Full Version : QTextEdit and inserting HTML



OnyxDragun
28th May 2014, 01:10
I have a QPlainTextEdit widget that I insert HTML into via moving the cursor along.

What I have noticed is that if there are things that are in < > tags but are NOT valid html elements, then QPlainTextEdit seems to fail.



QString txt = "You have the following commands: <br />read <#/next>, note <title>, remove #, list <from #/last>, marker <#/reset>";


When do the following


ui->txtOutput->moveCursor(QTextCursor::End);
ui->txtOutput->textCursor().insertHtml(txt);
ui->txtOutput->setTextCursor(prevCursor);


I get the following showing up in the QPlainTextEdit widget


You have the following commands:
read , note

Is is possible to some how.. have QPlainTextEdit ignore non-valid HTML elements? Then again, I'm not 100% sure this is my issue, but it seems iike it so far.

(on a side note, is there a [pre] for things that are not code?)

wysota
28th May 2014, 07:32
You want it to ignore invalid html tags but still respect valid ones? If so then you have to preprocess your text and replace all angle brackets with html entities.

anda_skoa
28th May 2014, 08:16
See http://qt-project.org/doc/qt-4.8/qt.html#escape for a helper function that escapes HTML entities

Cheers,
_

OnyxDragun
28th May 2014, 17:58
See http://qt-project.org/doc/qt-4.8/qt.html#escape for a helper function that escapes HTML entities

That was the key! Thanks! Now my MUD client supports ansi colours properly as it converts them to html for the QPlainTextEdit.