Parsing HTML with Qtwebkit under Windows

Source: Internet
Author: User

Environment

Windows7 + VS2010 + Qt5.2_opengl

Configuring the development environment

1. Installation VS2010

2, install QT 5.2

QT Website: http://qt-project.org/downloads

Download and install QT5.2:QT 5.2.1 for Windows 32-bit (VS, OpenGL, 517 MB)

3. Install visual Studio add-in for QT5

Download Address: Visual Studio add-in 1.2.2 for QT5

4, Configuration vs 2010

Configuration path: QT5 ==> "qt Options" ==> "qt versions" ==> ADD

The default path is: C:qtqt5.2.05.2.0msvc2010_opengl

Parsing HTML

1, the establishment of Qtwebkit project

Action path: VS2010 ==> File ==> New ==> Project ==> "Qt5 Projects" ==> "Qt Application"

Attention matters

Select the "WebKit" and "WebKit Widgets" options in "Project Settings":

2. Add header File

#include <QtGui> #include <QtWebKit> #include <QWebView>

3. Content Analysis

3.1, parsing the http form of the URL

Using the Qwebview SetUrl method, the example is as follows:

Qwebview view; View.show (); View.seturl (Qurl ("Http://www.cnblogs.com/mikezhang"));

3.2. Resolve Local Files

Using the Qwebview SetUrl method, the example is as follows:

Qwebview view; View.show (); View.seturl (Qurl ("file:///E:/tmp/1.html"));

3.3. Parse HTML string

Using the Qwebview sethtml method, the example is as follows:

Qapplication app (argc, argv); Qwebview view; View.show (); Std::ifstream fin ("e:/tmp/1.html"); Std::stringstream buffer; Buffer << fin.rdbuf (); Fin.close (); View.sethtml (Buffer.str (). C_STR ());

The complete code is as follows:

#include <QtGui> #include <QtWebKit> #include <QWebView> #include <fstream> #include < string> #include <sstream> int main (int argc, char *argv[]) {   qapplication app (argc, argv);   &N Bsp Qwebview view;    view.show ();    //method 1:a remote URL    //view.seturl (Qurl ("Http://www.cnblogs.com/mikezhang"));    //method 2:a The local URL    //view.seturl (Qurl ("file:///E:/tmp/1.html"));      //method 3:set HTML content      std::ifstream fin ("e:/tmp/1.html");      std::stringstream buffer;      buffer << fin.rdbuf ();      fin.close ();    view.sethtml (Buffer.str (). C_STR ());    return app.exec (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.