3

I'm working with the QT Creator and I would like to write a HTML code into a QString or to set a textEdit with setHtml(). The problem is, that I cannot really escape the special characters that come with the HTML code, e.g. :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC- html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } </style></head><body style=" font-family:'.Lucida Grande UI'; font-size:13pt; font-weight:400; font-style:normal;"> 

How can I write something like this to a QString or directly set it to the textEdit?

I need this because my HTML text may change.

2 Answers 2

3

There's an easy way for that! If you are using Qt5, just use

QString::toHtmlEscaped() 

Example (from here):

QString plain = "#include <QtCore>"; QString html = plain.toHtmlEscaped(); // html == "#include &lt;QtCore&gt;" 

If you are using Qt4:

Qt::escape 

Example:

QString plain = "#include <QtCore>"; Qstring html = Qt::escape(plain); 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, I already found that function. But the problem is: when I'm defining a QString with the HTML-code mentioned above, the QT underlines the string and says that it has found errors. Further, I think that the example is incorrect, isn't there a ";" missing at the end of the firs line (the error is also in the QT documentation).
This is strange...can you post a short snippet of code? Maybe, even others could help. Regarding the missing semicolon, I've spotted it, and I already edited the answer. Hope that we can find the issue.
Of course. It looks like this: QString test = "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; }"; The whole line is then underlined red and I cannot write further code after this line. If I do like in the example it shows me an error when building the program.
Can you show the exact error? By the way, I'm almost sure that the anomaly relies in those double quotes inside the string: the compiler interprets the QString this way: QString test = "<!DOCTYPE HTML PUBLIC " and the other charactes are left away, causing the error. Have you tried to explicity escaping the double quotes? I hope this won't interfere with toHtmlEscaped().
Is there any way to post a screenshot here? I figured something out: When I take the HTML-Code line by line, meaning that I put each line into a QString and escape the double quotes it works fine. But my HTML-Code is pretty long and I don't want to put every line into a variable. Is there maybe any other way to modify the text of a textEdit "by hand"? I mean to add bold text etc..
|
1

You are asking simply how to escape a C string. You'll need to use an online tool, such as http://www.digitalcoding.com/tools/addslashes-stripslashes.html, or a command line tool like echo '<!DOCTYPE HTML PUBLIC...' | sed -e 's-"-\"-g' > escaped.c, or any text editor's replace functionality - say NotePad++'s.

Using such a tool, your string becomes:

<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC- html40/strict.dtd\">rn<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">rnp, li { white-space: pre-wrap; }rn</style></head><body style=\" font-family:\'.Lucida Grande UI\'; font-size:13pt; font-weight:400; font-style:normal;\"> 

Just add a double quote at the beginning and at the end and you're all set:

auto foo = QStringLiteral("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC- html40/strict.dtd\">rn<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">rnp, li { white-space: pre-wrap; }rn</style></head><body style=\" font-family:\'.Lucida Grande UI\'; font-size:13pt; font-weight:400; font-style:normal;\">"); 

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.