当前位置: 代码迷 >> Web前端 >> 给web使用'加壳子':用QtWebKit直接改造现有应用
  详细解决方案

给web使用'加壳子':用QtWebKit直接改造现有应用

热度:339   发布时间:2012-10-24 14:15:58.0
给web应用'加壳子':用QtWebKit直接改造现有应用
引用

WebKit是Mac OS X v10.3及以上版本所包含的软件框架(对v10.2.7及以上版本也可通过软件更新获取)。同时,WebKit也是Mac OS X的Safari网页浏览器的基础。WebKit是一个开源项目,主要由KDE的KHTML修改而来并且包含了一些来自苹果公司的一些组件。

引用

QtWebKit是Apple公司的开源项目WebKit针对Qt的port

费话少说,直接上代码
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import PyQt4.QtNetwork
import sys



app,browser=None,None

class BrowserScreen(QWebView): 
    '''主窗口'''
    def __init__(self): 
        QWebView.__init__(self) 
        self.resize(800, 600) 
        self.show() 
        self.setHtml(open('test.html').read().decode('utf-8'))
#        self.load(QUrl(r'http://www.g.cn'))
    
    def showMessage(self, msg): 
        print msg


class PythonJS(QObject): 
    '''供js调用'''
    __pyqtSignals__ = ( "contentChanged(const QString &)" ) 
    
    @pyqtSignature("")
    def close(self):
        sys.exit()
    
    @pyqtSignature("") 
    def openMap(self): 
        browser.setHtml(open('test1.html').read().decode('utf-8'))
    
if __name__== '__main__' :
    app = QApplication(sys.argv) 
    browser = BrowserScreen() 
    #供js调用的python对象
    pjs = PythonJS() 
    #绑定通信对象
    browser.page().mainFrame().addToJavaScriptWindowObject( "python" , pjs) 
    QObject.connect(pjs , SIGNAL( "contentChanged(const QString &)" ), browser.showMessage) 
    sys.exit(app.exec_())

test.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Untitled Document</title>
	</head>
	<body>
		<button id='id1'>调用python</button>
		<button id='id2'>关闭</button>
		<iframe src="http://www.iteye.com/"
                   width="790" height="590"
                   scrolling="yes"
                   frameborder="0"
                   align="center"></iframe>
	</body>
	<script type="text/javascript" language="JavaScript">
			document.getElementById('id1').onclick=function(){
				python.openMap();
			}
			document.getElementById('id2').onclick=function(){
				python.close();
			}
	</script>
</html>


test1.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0px; padding: 0px }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var map
  function initialize() {
    var latlng = new google.maps.LatLng(40.02045,116.312428);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 
    google.maps.event.addListener(map, 'click', function(event) {
	    placeMarker(event.latLng);
	  });
	function placeMarker(location){
		var clickedLocation = new google.maps.LatLng(location);
		var marker = new google.maps.Marker({
			position: location,
			map: map
		});
		
		map.setCenter(location);
	}
  }
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>


下载地址:
http://d.namipan.com/d/e1fa9c704e5452b818904299c64917494fac0482b6d3a000