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=&
Source=s_q& HL=ZH-CN& Geocode=& q=%e6%b9%96%e5%8d%97&
aq=& sll=62.593341,100.546875& sspn=76.133466,302.34375&
brcurrent=3,0x3698154db56a6dd7:0x8afbac67256c824a,0%3b5,0,0&
Ie=utf8& hq=& hnear=%e6%b9%96%e5%8d%97%e7%9c%81&
ll=28.112444,112.98381&
spn=2.059913,4.724121& T=m& Z=8& Output=embed ">
</iframe>
<BR/><small>
<a href="http://ditu.google.cn/maps?f=q&
source=embed& HL=ZH-CN&
Geocode=& q=%e6%b9%96%e5%8d%97&
aq=& sll=62.593341,100.546875&
sspn=76.133466,302.34375&
brcurrent=3,0x3698154db56a6dd7:0x8afbac67256c824a,0%3b5,0,0&
Ie=utf8& hq=&
hnear=%e6%b9%96%e5%8d%97%e7%9c%81&
ll=28.112444,112.98381& spn=2.059913,4.724121&
T=m& 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 ();
}