6

I want to convert entity character(Escape character) to HTML in QT, please help me....

i.e: I want to replace " with ", > with >

=====This is my code that not worked====

 QString MyApp::ReplaceString(const QString Data, const QString &Before, const QString &After) { QString Result = Data; Result.replace(Before, After, Qt::CaseInsensitive); return Result; } 

========

QTextCodec *codec = QTextCodec::codecForName("UTF-8"); QByteArray data=pReply->readAll(); QString str = codec->toUnicode((const char *)data); str = Qt::escape(str); str = ReplaceString(str, """, "\""); str = ReplaceString(str,">", ">"); 
9
  • 1
    Your question is not clear at all. Please add some examples of what you want converted to what. Commented Oct 8, 2011 at 10:30
  • @Mat: Thanks for your reply, I've been add some code Commented Oct 8, 2011 at 10:42
  • Those replacements work. Are you sure you should be escapeing your input? Commented Oct 8, 2011 at 10:52
  • @Matt: I've been getting result from fa.wikipedia.org/w/… Commented Oct 8, 2011 at 11:02
  • Use an XML parser! Those results contain both escaped and unescaped symbols. Commented Oct 8, 2011 at 11:06

4 Answers 4

18

I'm not sure I understand what you want, just guessing. You can use QTextDocument. Try something like this:

QTextDocument text; text.setHtml("<>""); QString plain = text.toPlainText(); qDebug("%s.", qPrintable(plain)); 

Remember that QTextDocument needs the gui module.

Sign up to request clarification or add additional context in comments.

3 Comments

The reverse also works for creating HTML-encoded versions of plaintext strings. Nice!
I think it's worth mentioning that it introduces QtGui dependency, so not useful if you have a console app.
How about this kind of characters "\xE6\x97\xA0\xE8\xAE\xBF\xE9\x97\xAE\xE6\x9D\x83\xE9\x99\x90\". I know QUrl.decode can decode them, but when they are not in a URL but in html, this way seems not graceful. Edit: I just find a way -- QString::fromUtf8(data).
2

I think this will solve your problem.

QString escaped= QString(myhtml).replace("&","&amp;").replace(">","&gt;").replace("<","&lt;"); 

1 Comment

The OP asked for reverse convertion
1

Test escape() function:

 QString plain = "#include <QtCore>" QString html = Qt::escape(plain); // html == "#include &lt;QtCore&gt;" 

and convertFromPlainText() function:

QString Qt::convertFromPlainText ( const QString & plain, WhiteSpaceMode mode = WhiteSpacePre ) 

Comments

1

Hello to convert non ASCII character to &#XXX; (where XXX is a number):

/***************************************************************************//*! * @brief Encode all non ASCII characters into &#...; * @param[in] src Text to analyze * @param[in,opt] force Force the characters "list" to be converted. * @return ASCII text compatible. * * @note Original code: http://www.qtforum.org/article/3891/text-encoding.html * * @warning Do not forget to use QString::fromUtf8() */ QString encodeEntities( const QString& src, const QString& force=QString() ) { QString tmp(src); uint len = tmp.length(); uint i = 0; while( i<len ) { if( tmp[i].unicode() > 128 || force.contains(tmp[i]) ){ QString rp = "&#"+QString::number(tmp[i].unicode())+";"; tmp.replace(i,1,rp); len += rp.length()-1; i += rp.length(); }else{ ++i; } } return tmp; } /***************************************************************************//*! * @brief Allows decode &#...; into UNICODE (utf8) character. * @param[in] src Text to analyze * @return UNICODE (utf8) text. * * @note Do not forget to include QRegExp */ QString decodeEntities( const QString& src ) { QString ret(src); QRegExp re("&#([0-9]+);"); re.setMinimal(true); int pos = 0; while( (pos = re.indexIn(src, pos)) != -1 ) { ret = ret.replace(re.cap(0), QChar(re.cap(1).toInt(0,10))); pos += re.matchedLength(); } return ret; } 

Basic usage:

qDebug() << encodeEntities(QString::fromUtf8("éà@<>hello the world €"),QString("<>")); // Will print: &#233;&#224;@&#60;&#62;hello the world &#8364; qDebug() << decodeEntities("a&#223;&#233;plop&#233;&#224;&#231;&#234;&#8364;"); // Will print: hello world 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.