[PyQt] Are theer limits to the ability of QWebView to process javascript?

BJ bjtexas at swbell.net
Tue Jul 26 22:26:15 BST 2011


  ----- Original Message ----- 
  From: Vincent Vande Vyvre 
  To: pyqt at riverbankcomputing.com 
  Sent: Saturday, July 23, 2011 12:12 AM
  Subject: Re: [PyQt] Are theer limits to the ability of QWebView to process javascript?


  Le 23/07/11 03:12, Phlip a écrit : 
On Fri, Jul 22, 2011 at 1:14 PM, BJ <bjtexas at swbell.net> wrote:

I have a webpage containing quite a bit of javascript to display Google
Maps.
It seems to run well on IE and Firefox browsers but is unable to run in a
QWebView.
Can you report any error messages, or logging? Can you survey other
Ajax sites & see if any do or don't work?

Does any of the maps page show at all?

And recall that Google Maps depends on your cache to hold each
regional image tile. So does QWebView have, for example, a different
cache than real web browsers that use webkit?

This code is working:

  -------%<-----------------
  # -*- coding: utf-8 -*-

  import sys

  from PyQt4 import QtCore, QtGui
  from PyQt4 import QtWebKit

  class WebDialog(object):
      def setupUi(self, WebDlg):
          WebDlg.resize(671, 588)
          WebDlg.setWindowTitle("GeoTag")
          self.gridLayout = QtGui.QGridLayout(WebDlg)
          self.verticalLayout = QtGui.QVBoxLayout()
          self.webView = QtWebKit.QWebView(WebDlg)
          self.verticalLayout.addWidget(self.webView)
          self.horizontalLayout_2 = QtGui.QHBoxLayout()
          self.horizontalLayout = QtGui.QHBoxLayout()
          self.link_led = QtGui.QLineEdit(WebDlg)
          self.link_led.setMinimumSize(QtCore.QSize(400, 0))
          self.horizontalLayout.addWidget(self.link_led)
          self.get_coord_btn = QtGui.QToolButton(WebDlg)
          self.get_coord_btn.setText("Get coord.")
          self.horizontalLayout.addWidget(self.get_coord_btn)
          self.progressBar = QtGui.QProgressBar(WebDlg)
          self.progressBar.setProperty("value", 0)
          self.progressBar.setTextVisible(False)
          self.horizontalLayout.addWidget(self.progressBar)
          self.close_btn = QtGui.QPushButton(WebDlg)
          self.close_btn.setText("Close")
          self.horizontalLayout.addWidget(self.close_btn)
          self.verticalLayout.addLayout(self.horizontalLayout)
          self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)
          self.webPage = QtWebKit.QWebPage()
          self.webView.setPage(self.webPage)        
          self.webView.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
          self.webView.loadProgress.connect(self.progress)
          self.webView.urlChanged.connect(self.set_link)
          self.webPage.contentsChanged.connect(self.pos_changed)
          frame = self.webPage.mainFrame()
          self.get_coord_btn.clicked.connect(self.pos_changed)
          self.close_btn.clicked.connect(WebDlg.accept)
          QtCore.QMetaObject.connectSlotsByName(WebDlg)

          self.set_link("mapViewer.html")
          self.load_page()

      def set_link(self, url):
          if not isinstance(url, str):
              url = url.toString()
          self.link_led.setText(url)

      def load_page(self):
          url = str(self.link_led.text())
          self.webView.setUrl(QtCore.QUrl(url))

      def progress(self, val):
          self.progressBar.setValue(val)

      def resize_event(self, event):
          print "Geometry %s" % self.webView.geometry()
          QtWebKit.QWebView.resizeEvent(self.webView, event)

      def pos_changed(self, event):
          frame = self.webPage.mainFrame()
          document = frame.documentElement()
          datas = document.findAll("div")
          print "found %s 'div'" % datas.count()
          dat = frame.findFirstElement("div#latlon")
          print dat, dat.toPlainText()
          dat.setFocus()

  if __name__ == "__main__":
      import sys
      app = QtGui.QApplication(sys.argv)
      WebDlg = QtGui.QDialog()
      ui = WebDialog()
      ui.setupUi(WebDlg)
      WebDlg.show()
      sys.exit(app.exec_())

  ----%<-------------------------
  and the javascript

  mapViewer.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">


    function updateLatLonFields(lat, lon) {
      document.getElementById("latlon").innerHTML = lat + ', ' + lon;
    }

    function initialize() {
      var latlng = new google.maps.LatLng(-34.397, 150.644);
      var myOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      var map = new google.maps.Map(document.getElementById("map_canvas"),
          myOptions);

      document.getElementById("latlon").innerHTML = latlng.lat() + ",  " + latlng.lng();

      google.maps.event.addListener(map, 'bounds_changed', function() {
          var center = map.getCenter();
          updateLatLonFields(center.lat(), center.lng());
          });

    }

  </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
    <div id="latlon"></div>
  </body>
  </html>

  --------%<--------------------------------

  Same results with OpenStreetMap

  Regards



  -- 
  Vincent V.V.
  Oqapy . Qarte+7 . PaQager


------------------------------------------------------------------------------


  _______________________________________________
  PyQt mailing list    PyQt at riverbankcomputing.com
  http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Thanks for the example Vincent, I found my problem after looking at your
example.  However there seems to be some functionality issues. I can't
pan with the cursor or get my infowindows to work.

Thanks for your response.

Bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20110726/f1203469/attachment-0001.html>


More information about the PyQt mailing list