【问题标题】:How can I make a QString html-escaped如何使 QString html 转义
【发布时间】:2013-01-05 15:17:34
【问题描述】:

如何转义/清理包含 HTML 的 QString?

showInBroswser(escaped(str)) == showInNotepad(str);

【问题讨论】:

    标签: c++ html qt html-escape


    【解决方案1】:

    Qt 5

    使用QString::toHtmlEscaped()

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

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

    Qt 4

    使用Qt::escape

    #include <QtGui/qtextdocument.h>
    
    QString src;
    Qstring html = Qt::escape(src);
    showInBrowser(html) == showInNotepad(str);
    

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

    【讨论】:

    • 对我不起作用,因为它不编码像 ä 这样的德语特殊字符。有没有更好的函数可以识别所有特殊字符?
    • 你如何扭转这种局面?即,没有QString::fromHtmlEscaped()
    【解决方案2】:

    为了让这个答案与时俱进,Qt 5.1 有QString::toHtmlEscaped()

    【讨论】:

      【解决方案3】:

      如果你想在 QTextEdit 中插入纯文本,你可以使用:

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

      例如,修改颜色:

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

      对于文本的字体或其他属性也类似...

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-30
        • 2021-11-19
        • 1970-01-01
        • 2011-03-03
        • 1970-01-01
        • 2021-11-17
        相关资源
        最近更新 更多