How can I make a QString html-escaped

2019-02-11 12:00发布

How do I escape/sanitize a QString that contains HTML?

I.e. showInBroswser(escaped(str)) == showInNotepad(str);

3条回答
爷、活的狠高调
2楼-- · 2019-02-11 12:46

If you want to insert plain text to a QTextEdit you can use:

void QTextEdit::insertPlainText ( const QString & text );

and, for example, to modify the color :

void QTextEdit::setTextColor ( const QColor & c ); 

And similar for the font or other property of the text...

Hope that helps.

查看更多
小情绪 Triste *
3楼-- · 2019-02-11 12:55

Qt 5

Use QString::toHtmlEscaped()

QString src;
Qstring html = src.toHtmlEscaped();
showInBrowser(html) == showInNotepad(str);

Reference: http://doc.qt.io/qt-5/qstring.html#toHtmlEscaped

Qt 4

Use Qt::escape.

#include <QtGui/qtextdocument.h>

QString src;
Qstring html = Qt::escape(src);
showInBrowser(html) == showInNotepad(str);

Reference: http://doc.qt.io/qt-4.8/qt.html#escape

查看更多
Evening l夕情丶
4楼-- · 2019-02-11 12:59

Just to bring this answer up with the times, Qt 5.1 has QString::toHtmlEscaped().

查看更多
登录 后发表回答