Handling HTML: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
(Convert ExpressionEngine section headers)
Line 38: Line 38:
| [http://www.chilkatsoft.com/C+''-HTML-Parser.asp '''Chilkat'''] | C''+ library | Win, Mac, Linux, … | {color:#458}proprietary |
| [http://www.chilkatsoft.com/C+''-HTML-Parser.asp '''Chilkat'''] | C''+ library | Win, Mac, Linux, … | {color:#458}proprietary |


h3. Manual HTML processing
=== Manual HTML processing ===
 
For specialized HTML parsers with a similarly low-level API as Qt's XML handling classes, refer to third-party C/C++ libraries, e.g.:
For specialized HTML parsers with a similarly low-level API as Qt's XML handling classes, refer to third-party C/C++ libraries, e.g.:



Revision as of 15:57, 5 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

[toc align_right="yes" depth="2"]

Handling HTML

This page discusses various available options for working with HTML documents in your Qt application. Please also read the general considerations outlined on the Handling Document Formats page.

p{width:60%;border:solid 1px #99a;background:#eef;color:#335;padding:2pt 4pt;font-size:0.9em;line-height:150%;font-style:italic}. Note that this information is collaboratively collected by the community, with no promise of completeness or correctness. In particular, use your own research and judgment when evaluating third-party libraries or tools!

Reading / Writing

Scribe

Qt's Scribe framework (see Handling Document Formats) has built-in support for loading from / saving to HTML (see "QTextDocument::setHtml":/doc/qt-4.8/qtextdocument.html#setHtml and "toHtml":/doc/qt-4.8/qtextdocument.html#toHtml as well as Doc:QTextDocumentWriter). Together with the format-independent API that Doc:QTextDocument provides for modifying documents (or creating them from scratch), this makes Scribe an adequate framework for processing or generating HTML documents.

However, it only supports a "limited subset of static HTML 4 / CSS 2.1":/doc/qt-4.8/richtext-html-subset.html - corresponding to the limited set of built-in document features which QTextDocument supports internally.

QtWebKit

The Webkit-based web browser framework shipped with Qt provides the Doc:QWebPage and Doc:QWebFrame classes, which can be used to load an HTML document (or any web page) without actually rendering it, and access or modify it through a DOM-like API. Saving back to HTML is possible using "QWebFrame::toHtml":/doc/qt-4.8/qwebframe.html#toHtml.

Keep in mind though that loading an HTML document in this way will not just passively parse it (like the Scribe framework or the XML/HTML parsers discussed below would), but actively evaluate it like a browser would, i.e. loading linked content like iframes, running JavaScript that is set to run on start-up, etc. Whether this is useful or problematic will depend on your specific use-case.

p{border:dashed 1px #a94;background:#fbf3dd;color:#530;padding:2pt 4pt;margin-left:2pt;margin-right:2pt;font-size:0.9em;line-height:150%;font-style:italic}. TODO: Can someone confirm whether this is true (and unavoidable), and edit this section accordingly?

Manual XML processing

If your application needs to parse or write HTML/XHTML documents which are valid XML, consider processing them using Qt's XML handling classes (see Handling Document Formats).

Note that there are third-party tools/libraries available for automatically converting "normal" (and even broken) HTML documents into valid XHTML/XML which is suitable for processing:

table{width:95%;margin-left:2.5%}. | |. type |. platforms |. license | | HTML Tidy | stand-alone tool | Win, Mac, Linux, … | MIT-like {color:#458}[permissive] | | TidyLib | C library | Win, Mac, Linux, … | MIT-like {color:#458}[permissive] | | -HTML-Parser.asp Chilkat | C+ library | Win, Mac, Linux, … | {color:#458}proprietary |

Manual HTML processing

For specialized HTML parsers with a similarly low-level API as Qt's XML handling classes, refer to third-party C/C++ libraries, e.g.:

table{width:95%;margin-left:2.5%}. | |. API |. parsing |. writing |. parsing modes |. platforms |_. license | | libxml2 | C | {color:#580}yes | {color:#920}? | stream, SAX, DOM (non-validating?) | Win, Mac, Linux, … | MIT {color:#458}[permissive] | | htmlcxx | C++ | {color:#580}yes | {color:#580}yes | SAX, DOM, ? (non-validating) | Win, Linux, ? | LGPL {color:#458}[weak copyleft]| | libhtml | C | {color:#580}yes | {color:#580}yes | stream (strongly-validating) | Linux, ? | ICS {color:#458}[permissive] |

Rendering / Interactive Viewing

Scribe

As already described above, Qt's Scribe framework supports automatically importing HTML content into a Doc:QTextDocument. Once in that form, you can…

  • …render it onto any Doc:QPaintDevice using "QTextDocument::drawContents":/doc/qt-4.8/qtextdocument.html#drawContents.
  • …show it to the user through a Doc:QTextEdit widget (either in read-only mode, or in editable mode which allows the user to actually edit the document interactively).

Again, the restriction to the "limited subset of static HTML 4 / CSS 2.1":/doc/qt-4.8/richtext-html-subset.html supported by QTextDocument applies.

QtWebKit

[[Image:{float:right;margin:0 1em 1em 1em}/doc/qt-4.8/images/webkit-examples.png|]] If you need more powerful viewing / user-interaction capabilities, take a look at the "QtWebKit browser framework":/doc/qt-4.8/qtwebkit.html which is included with Qt. It can interactively display pretty much any modern web document (which may make use of HTML 5, XHTML, CSS 3, SVG, JavaScript, plugins like Flash, etc.). The viewer component is available in the following forms:

The framework also allows rendering an HTML page to any Doc:QPaintDevice using "QWebFrame::render":/doc/qt-4.8/qwebframe.html#render.

p{color:#fff;border-bottom:solid 1px #ccc;clear:both}. .

See Also