码迷,mamicode.com
首页 > Web开发 > 详细

Qt之WebKit学习之QWebView显示网页与google地图

时间:2014-07-26 01:05:37      阅读:863      评论:0      收藏:0      [点我收藏+]

标签:des   cWeb   style   blog   http   color   使用   os   

目录  一:Qt通过地址显示网页

    二:Qt调用setHtml方法加载html格式的网页,以加载google地图插件为例.

 

一.Qt显示网页只要三步

  1) 新建QWebView对象:QWebView *view = new QWebView(this);

  2) 调用setUrl或load函数设置要显示的网页地址,如load("http://www.baidu.com"); 必须以http://开头;

  3)调用show函数显示。

完整代码:

webview.h中:

  #ifndef WEBVIEW_H

#define WEBVIEW_H

#include <QWidget>
#include <QWebView>

class WebView : public QWebView
{
    Q_OBJECT
public:
    explicit WebView(QWebView *parent = 0);
};
#endif // WEBVIEW_H

webview.cpp中:

#include "webview.h"

#include <QWebView>  //pro中要加webkitwidgets
#include <QFile>

WebView::WebView(QWebView *parent) :
    QWebView (parent)
{

QWebView *view = new QWebView(this);
    view->load(QUrl("http://www.tudou.com/"));
    view->show();
}
效果如图:

bubuko.com,布布扣

 

二.QWebView使用setHtml函数加载html格式的网页,以加载google地图插件为例

  1) 获取google地图插件的代码:使用浏览器进入google地图,点左上角bubuko.com,布布扣(分享链接)bubuko.com,布布扣,复制第二个方框的代码:如图红色圈住的地方bubuko.com,布布扣

  2) 获取的代码为<iframe width="720" height="450" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://ditu.google.cn/maps?f=q&amp;

    source=s_q&amp;hl=zh-CN&amp;geocode=&amp;q=%E6%B9%96%E5%8D%97&amp;
    aq=&amp;sll=62.593341,100.546875&amp;sspn=76.133466,302.34375&amp;
    brcurrent=3,0x3698154db56a6dd7:0x8afbac67256c824a,0%3B5,0,0&amp;
    ie=UTF8&amp;hq=&amp;hnear=%E6%B9%96%E5%8D%97%E7%9C%81&amp;
    ll=28.112444,112.98381&amp;
    spn=2.059913,4.724121&amp;t=m&amp;z=8&amp;output=embed">
</iframe>
<br /><small>
    <a href="http://ditu.google.cn/maps?f=q&amp;
    source=embed&amp;hl=zh-CN&amp;
    geocode=&amp;q=%E6%B9%96%E5%8D%97&amp;
    aq=&amp;sll=62.593341,100.546875&amp;
    sspn=76.133466,302.34375&amp;
    brcurrent=3,0x3698154db56a6dd7:0x8afbac67256c824a,0%3B5,0,0&amp;
    ie=UTF8&amp;hq=&amp;
    hnear=%E6%B9%96%E5%8D%97%E7%9C%81&amp;
    ll=28.112444,112.98381&amp;spn=2.059913,4.724121&amp;
    t=m&amp;z=8" style="color:#0000FF;text-align:left">查看大图
    </a>
</small>
可以删去br、small和a标记,最后在代码前后添加<html></html>标记
将文件保存为*.html(注:删除的部分为屏蔽google地图的右键效果,也可以保留)。

3) 调用QWebView的setHtml函数,不能直接使用html文件的路径,原因如下红色代码。

举例:
  main.cpp:
  #include <QApplication>
#include "webview.h"
#include <QWebView>
#include <QFile>
#include <QMainWindow>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv)
    QMainWindow window;
    QWebView webview(&window);
    QFile source("test03.html");
    source.open(QIODevice::ReadOnly);
    webview.setHtml(QString::fromUtf8(source.readAll().constData())); //必须这样写
    window.setCentralWidget(&webview);
    window.show();
    return app.exec();
}

 

 

Qt之WebKit学习之QWebView显示网页与google地图,布布扣,bubuko.com

Qt之WebKit学习之QWebView显示网页与google地图

标签:des   cWeb   style   blog   http   color   使用   os   

原文地址:http://www.cnblogs.com/minglicnblogs/p/3868748.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!