Advertisement
takumotanji

example webkit browser

Sep 26th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork
  2. from PyQt4.QtWebKit import QWebSettings
  3.  
  4. QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
  5. QWebSettings.globalSettings().setAttribute(QWebSettings.JavascriptEnabled, True)
  6. QWebSettings.globalSettings().setAttribute(QWebSettings.JavaEnabled, True)
  7. QWebSettings.globalSettings().setAttribute(QWebSettings.JavascriptCanOpenWindows, True)
  8. QWebSettings.globalSettings().setAttribute(QWebSettings.JavascriptCanCloseWindows, True)
  9. QWebSettings.globalSettings().setAttribute(QWebSettings.JavascriptCanAccessClipboard, True)
  10. QWebSettings.globalSettings().setAttribute(QWebSettings.SpatialNavigationEnabled, True)
  11. QWebSettings.globalSettings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
  12. QWebSettings.globalSettings().setAttribute(QWebSettings.PrintElementBackgrounds, True)
  13. QWebSettings.globalSettings().setAttribute(QWebSettings.SiteSpecificQuirksEnabled, True)
  14. QWebSettings.globalSettings().setAttribute(QWebSettings.LocalStorageEnabled, True)
  15. QWebSettings.globalSettings().setAttribute(QWebSettings.OfflineWebApplicationCacheEnabled, True)
  16.  
  17. QWebSettings.globalSettings().setAttribute(QWebSettings.LocalContentCanAccessRemoteUrls, True)
  18. QWebSettings.globalSettings().setAttribute(QWebSettings.LocalContentCanAccessFileUrls, True)
  19. QWebSettings.globalSettings().setAttribute(QWebSettings.DnsPrefetchEnabled, True)
  20. QWebSettings.globalSettings().enablePersistentStorage()
  21. cookieJar = QtNetwork.QNetworkCookieJar()
  22.  
  23. networkAccessManager = QtNetwork.QNetworkAccessManager()
  24. networkAccessManager.setCookieJar(cookieJar)
  25.  
  26. class myWebView(QtWebKit.QWebView):
  27.     _windows = set()
  28.  
  29.     def __init__(self, parent=None):
  30.         super(myWebView, self).__init__(parent)
  31.        
  32.         self.page().setNetworkAccessManager(networkAccessManager)
  33.  
  34.         self.load(QtCore.QUrl("http://www.google.com"))
  35.  
  36.     @classmethod
  37.     def _removeWindow(cls, window):
  38.         if window in cls._windows:
  39.             cls._windows.remove(window)
  40.  
  41.     @classmethod
  42.     def newWindow(cls):
  43.         window = cls()
  44.         cls._windows.add(window)
  45.         return window
  46.  
  47.     def closeEvent(self, event):
  48.         self._removeWindow(self)
  49.         event.accept()
  50.  
  51.     def createWindow(self, webWindowType):
  52.         window = self.newWindow()
  53.         if webWindowType == QtWebKit.QWebPage.WebModalDialog:
  54.             window.setWindowModality(QtCore.Qt.ApplicationModal)
  55.  
  56.         window.show()
  57.  
  58.         return window
  59.  
  60. class myWindow(QtGui.QMainWindow):
  61.     def __init__(self, parent=None):
  62.         super(myWindow, self).__init__(parent)
  63.  
  64.         self.centralwidget = QtGui.QWidget(self)
  65.  
  66.         self.webView = myWebView(self.centralwidget)
  67.  
  68.         self.pushButton = QtGui.QPushButton(self.centralwidget)
  69.         self.pushButton.setText("New Window")
  70.         self.pushButton.clicked.connect(lambda: self.webView.createWindow(QtWebKit.QWebPage.WebBrowserWindow))
  71.  
  72.         self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
  73.         self.verticalLayout.addWidget(self.pushButton)
  74.         self.verticalLayout.addWidget(self.webView)
  75.  
  76.         self.setCentralWidget(self.centralwidget)
  77.  
  78. if __name__ == "__main__":
  79.     import  sys
  80.  
  81.     app  = QtGui.QApplication(sys.argv)
  82.     main = myWindow()
  83.     main.show()
  84.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement