windows下用QTwebkit解析html

來源:互聯網
上載者:User

   環境

  windows7 + VS2010 + QT5.2_opengl

  配置開發環境

  1、安裝VS2010

  2、安裝QT 5.2

  QT網站:http://qt-project.org/downloads

  下載並安裝QT5.2:Qt 5.2.1 for Windows 32-bit (VS 2010, OpenGL, 517 MB)

  3、安裝Visual Studio Add-in for QT5

  下載地址:Visual Studio Add-in 1.2.2 for Qt5

  4、配置VS 2010

  配置路徑:QT5 ==> "Qt Options" ==> "Qt Versions" ==> Add

  預設路徑為:C:QtQt5.2.05.2.0msvc2010_opengl

  解析html

  1、建立QtWebkit工程

  操作路徑:VS2010 ==> File ==> New ==> Project ==> "Qt5 Projects" ==> "Qt Application"

  注意事項

  在"Project Settings"裡面選中“WebKit”和“Webkit Widgets”選項:

  2、添加標頭檔

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

  3、解析內容

  3.1、解析http形式的url

  使用QWebView的setUrl方法,樣本如下:

  QWebView view; view.show(); view.setUrl(QUrl("http://www.cnblogs.com/mikezhang"));

  3.2、解析本地檔案

  使用QWebView的setUrl方法,樣本如下:

  QWebView view; view.show(); view.setUrl(QUrl("file:///E:/tmp/1.html"));

  3.3、解析html字串

  使用QWebView的setHtml方法,樣本如下:

  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());

  完整代碼如下:

 #include <QtGui>#include <QtWebKit>#include <QWebView>#include <fstream>#include <string>#include <sstream> int main(int argc, char *argv[]) {    QApplication app(argc, argv);    QWebView view;    view.show();    // Method 1 : a remote url    //view.setUrl(QUrl("http://www.cnblogs.com/mikezhang"));    // Method 2 : a 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(); }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.