[PyQt] QWebPage don't execute javascript anymore
Vincent Vande Vyvre
vincent.vande.vyvre at telenet.be
Thu May 25 13:39:31 BST 2017
Hi,
Is it a change known into QWebPage with javascript content ?
The code below don't work (the map is not displayed) since my last
update to
Qt-5.5.1O and PyQt-5.5.1
Note: I can't change my code for QtWebEngineView.QWebEnginePage because
QtWebEngineView is not provided for Debian/Ubuntu.
mapviewer.py
---------------------------------------------------------
import sys
from PyQt5.QtCore import pyqtSignal, QUrl, Qt
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
from PyQt5.QtWidgets import QApplication
class MapViewer(QWebView):
coordinateChanged = pyqtSignal(list)
def __init__(self, parent=None):
super().__init__(parent)
self.page_ = QWebPage()
self.setPage(self.page_)
self.loadFinished.connect(self.on_load_finished)
url = QUrl.fromLocalFile('/NEED/FULL/PATH/ol.html')
self.setUrl(url)
def go_to(self, lat, lon):
self.page_.mainFrame().evaluateJavaScript(
'centerOnLatLon({0}, {1}, {2});'.format(lat, lon, 16))
self.on_load_finished()
def on_load_finished(self):
evl =
self.page_.mainFrame().evaluateJavaScript("updateLatLonFields();")
print("Map loaded at coord:", evl)
if __name__ == '__main__':
app = QApplication([])
mv = MapViewer()
mv.show()
mv.go_to(50.19345, 3.479976)
sys.exit(app.exec_())
------------------------------------------------------------
And the basic OpenLayer html (same problem with GoogleMap)
ol.html
------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<title>Accessible Map</title>
<link rel="stylesheet"
href="https://openlayers.org/en/v4.1.1/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v4.1.1/build/ol.js"></script>
</head>
<body>
<a class="skiplink" href="#map">Go to map</a>
<div id="map" class="map" tabindex="0"></div>
<button id="zoom-out">Zoom out</button>
<button id="zoom-in">Zoom in</button>
<script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
controls: ol.control.defaults({
attributionOptions: /** @type
{olx.control.AttributionOptions} */ ({
collapsible: false
})
}),
view: new ol.View({
center: ol.proj.fromLonLat([5.5, 50.5]),
zoom: 10
})
});
document.getElementById('zoom-out').onclick = function() {
var view = map.getView();
var zoom = view.getZoom();
view.setZoom(zoom - 1);
};
document.getElementById('zoom-in').onclick = function() {
var view = map.getView();
var zoom = view.getZoom();
view.setZoom(zoom + 1);
};
function updateLatLonFields() {
var view = map.getView();
var center = view.getCenter();
var lonLat = ol.proj.toLonLat(center);
var zoom = view.getZoom();
var values = [lonLat[0], lonLat[1], zoom];
return (values);
};
function centerOnLatLon(lat, lon, zoom) {
var x = parseFloat(lon);
var y = parseFloat(lat);
var centerAt = [x, y];
var newPos = ol.proj.fromLonLat(centerAt);
var view = map.getView();
view.setCenter(newPos);
view.setZoom(zoom);
};
</script>
</body>
</html>
------------------------------------------------------------------
Vincent
More information about the PyQt
mailing list