QT WebKit Learning qwebview show pages and Google Maps

Source: Internet
Author: User

Directory One: QT displays Web pages by address

Two: Qt calls the Sethtml method to load the HTML-formatted Web page to load the Google Maps plugin as an example.

One. qt Display Web page only three steps

1) New Qwebview object: Qwebview *view = new Qwebview (this);

2) Call the SetUrl or load function to set the address of the Web page to be displayed, such as load ("http://www.baidu.com"); Must start with http://;

3) Call the show function to display.

Full code:

In Webview.h:

  #ifndef Webview_h

#defineWebview_h
#include<QWidget>
#include<QWebView>
ClassWebView:publicqwebview   
{
    Q_object
Public:
    ExplicitWebView(qwebview*parent=0);      
};
#endif//webview_h 

In Webview.cpp:

#include "webview.h"

  add webkitwidgets to #include <QWebView>//pro
#include<QFile>
WebView::WebView(qwebview*parent):       
    Qwebview(parent)  
 {

qwebvi Ew *view = new qwebviewthis);
    View-load(qurl("http://www.tudou.com/"));      
    View-and show();  
}
Effect

Two. Qwebview use the sethtml function to load HTML-formatted Web pages to load the Google Maps plugin as an example

1) Get the code for the Google Maps plugin: Use your browser to access Google Maps, click the top left corner (share link), and copy the code for the second box: the place where the red circle lives.

2) Gets the code <iframe Width= height=< Span style= "color: #aa0000;" > " Frameborder=" 0 " Scrolling= Marginheight= marginwidth= Src=&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 "> View larger image    
    </a>
</small>
You can delete the BR, small, and a markers, and then add 
Save the file as *.html (note: The deleted part is the right-click effect for masking Google Maps, or it can be retained).

3) Call Qwebview's sethtml function, cannot use the path of the HTML file directly, for the following red code.

Example:
Main.cpp:
  #include<QApplication>
#include"Webview.h"
#include<QWebView>
#include<QFile>
#include<QMainWindow>
intmain(intargc,Char*argv[])       
{
    Qapplicationapp(argc,argv)     
    Qmainwindow window;
    Qwebview WebView (&window);
    QFile Source ("test03.html");
    Source.open (qiodevice::readonly);
    Webview.sethtml (Qstring::fromutf8 (Source.readall (). Constdata ())); Must be written like this
    Window.setcentralwidget (&webview);
    Window.show ();
    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.