转载自:http://www.cnblogs.com/gisbeginner/archive/2012/11/10/2763997.html
(1) 让qwebview 设置代理服务器
QNetworkProxyproxy;
proxy=QNetworkProxy::HttpProxy;
proxy.setHostName(tr("15.12.110.252"));//代理服务器
proxy.setPort(8080);
netwkAcssMng=new QNetworkAccessManager;
netwkAcssMng->setProxy(proxy);
webV=qFindChild<QWebView*>(this,"webView");//全局函数
webV->page()->setNetworkAccessManager(netwkAcssMng);
webV->load(QUrl("http://www.google.com.hk/"));
webV->show();
(2) 给qtwebkit 创建 插件
#include"intelliplugin.h"
#include"shineprint.h"
#include<QDebug>
#include<QWebView>
IntelliPlugin::IntelliPlugin(QWidget*parent):
QWebPluginFactory(parent)
{
qDebug()<<"IntelliPlugin::IntelliPlugin()"<<endl;
MimeTypemime;
mime.name="application/print-plugin";
m_mimeType.append(mime);
}
IntelliPlugin::~IntelliPlugin()
{
}
QObject*IntelliPlugin::create(constQString&mimeType,constQUrl&,constQStringList&argumentNames,constQStringList&argumentValues)const
{
qDebug()<<"IntelliPlugin::create()"<<endl;
foreach(MimeTypemime,this->m_mimeType){
if(!mime.name.isEmpty()&&mime.name==mimeType){
if(mimeType=="application/print-plugin"){
qDebug()<<"createshineprint"<<endl;
//写入自己的类
ShinePrint*pShinePrint=newShinePrint();
qDebug()<<"finishcreateshineprint."<<endl;
returnpShinePrint;
}
}
}
returnNULL;
}
QList<IntelliPlugin::Plugin>IntelliPlugin::plugins()const
{
Pluginplugin;
plugin.name="application/print-plugin";
plugin.description="JustforPrintMessagecalltest";
plugin.mimeTypes.append(m_mimeType);
QList<IntelliPlugin::Plugin>plugList;
plugList.append(plugin);
returnplugList;
}
shineprint.cpp 类
ShinePrint::ShinePrint(QWidget*parent):
QLabel(parent)
{
qDebug()<<"ShinePrint::ShinePrint()"<<endl;
}
ShinePrint::~ShinePrint()
{
qDebug()<<"ShinePrint::~ShinePrint()"<<endl;
}
voidShinePrint::PrintMessag(QStringmsg)
{
QMessageBoxmsgBox;
//QFontfont;
//font.setPixelSize(30);
//msgBox.setFont(font);
msgBox.setText(msg);
msgBox.setInformativeText("Justfortest!");
msgBox.setStandardButtons(QMessageBox::Save|QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
msgBox.exec();
qDebug()<<"JavaAlert()JavaScriptWillbecalled"<<endl;
//view->page()->mainFrame()->evaluateJavaScript("JavaAlert();");
qApp->exit(0);
}
(3) 让javascript 调用c++ 类
http://www.linuxidc.com/Linux/2012-04/58359.htm
<object type="application/print-plugin" data="http://www.sohu.com" width="1" height="1" id="Callobject">
</object>
function print() {
var s5 = "请耐心等待,过号请到分诊台重新分诊,谢谢合作";
/*调用的Qt中的方法: class ShinePrint::void PrintMessag(QString msg)*/
var printResult = document.getElementById("Callobject").PrintMessag(s5);
if (printResult < 0)
alert("网络出现故障,打印失败,请联系管理员");
}
<button onclick="print()">clickqtjavascript </button>
webview 进行必要的设置
ui->webView->settings()->setAttribute(QWebSettings::PluginsEnabled,true);
ui->webView->settings()->setAttribute(QWebSettings::JavascriptEnabled,true);
ui->webView->page()->setPluginFactory(newIntelliPlugin);
(4) 当一个load一个页面 ,希望通过点击这个页面的2级连接来做其他事情:,其中需要在 load之后
ui->webView->load(QUrl("http://www.baidu.com"));
ui->webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
需要给webveiw 的siganal (linkClicked) 加个槽函数
voidMainWindow::on_webView_linkClicked(constQUrl&arg1)
{
ui->webView->load(QUrl("http://www.qq.com"));
QMessageBoxhimessagebox;
himessagebox.setText("hitodayisaniceday");
himessagebox.exec();
}// 这样就行了
打开本地 html 文件
QStringfilename=QFileDialog::getOpenFileName(this,tr("打开文件..."),QString(),tr("网页文件(*.htm*.mht*.html);;AllFiles(*.*)"));
if(!filename.isEmpty())
{
ui->webView->settings()->setAttribute(QWebSettings::PluginsEnabled,true);
ui->webView->settings()->setAttribute(QWebSettings::JavascriptEnabled,true);
ui->webView->page()->setPluginFactory(newIntelliPlugin);
ui->webView->load(QUrl::fromUserInput(filename));
QStringcurFile=QFileInfo(filename).canonicalFilePath();//获得文件的标准路径
this->setWindowTitle(curFile+"-WebView");
//this->statusLabel->setText("");
}
隐藏qwebview 的滑动条
ui.webView->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff);
ui.webView->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlwaysOff);
ui.webView_2->page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal,Qt::ScrollBarAlwaysOff);
ui.webView_2->page()->mainFrame()->setScrollBarPolicy(Qt::Vertical,Qt::ScrollBarAlwaysOff);
qt 启动其他默认浏览器打开一个网页
Qt.openUrlExternally("http://yoururl")
Qt.openUrlExternally("tel:<number>")
QDesktopServices::openUrl(arg1);
qt 调用html页面的javascript
ui.webView->page()->mainFrame()->evaluateJavaScript("helloworld()"); //helloworld是html 里面的自定义javascript 方法
任何获得焦点的控件 不论是qt自己的控件还是html页面里德控件都会 获得剪切板里面的文字
ui.webView->page()->setContentEditable(true);
ui.webView->page()->triggerAction(QWebPage::Paste,true);