2013-01-03 79 views
1

我正在嘗試創建一個在所有打開的窗口中全局使用的cookie jar,以便創建的任何cookie都可用。如果我右鍵單擊一個鏈接並在新窗口中打開,然後關閉該窗口,當我點擊第一個窗口中的鏈接時,就會出現段錯誤。將Cookie jar分配給QWebView時出現分段錯誤

我想這是因爲它正在關閉的窗口中查找cookie jar。所以如何正確實施這些提示將是一個很好的開始。

from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork 

cookies = QtNetwork.QNetworkCookieJar() 

class Browser(QtWebKit.QWebView): 
    _windows = set() 

    @classmethod 
    def _removeWindow(cls, window): 
     if window in cls._windows: 
      cls._windows.remove(window) 

    @classmethod 
    def newWindow(cls): 
     window = cls() 
     cls._windows.add(window) 
     return window 

    def __init__(self, parent=None): 
     QtWebKit.QWebView.__init__(self, parent) 
     self.setAttribute(QtCore.Qt.WA_DeleteOnClose, True) 
     self.settings().setAttribute(
      QtWebKit.QWebSettings.JavascriptEnabled, True) 
     self.settings().setAttribute(
      QtWebKit.QWebSettings.JavascriptCanOpenWindows, True) 
     self.page().networkAccessManager().setCookieJar(cookies) 

    def closeEvent(self, event): 
     self._removeWindow(self) 
     event.accept() 

    def createWindow(self, mode): 
     window = self.newWindow() 
     if mode == QtWebKit.QWebPage.WebModalDialog: 
      window.setWindowModality(QtCore.Qt.ApplicationModal) 
     window.show() 
     return window 

if __name__ == '__main__': 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    browser = Browser() 
    browser.setUrl(QtCore.QUrl("http://google.com")) 
    browser.show() 
    sys.exit(app.exec_()) 

回答

1

嘗試QNetworkAccessManager.setCookieJar得到管理餅乾,像這樣的東西QNetworkCookieJar的手柄:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork 

cookieJar = QtNetwork.QNetworkCookieJar() 

networkAccessManager = QtNetwork.QNetworkAccessManager() 
networkAccessManager.setCookieJar(cookieJar) 

class myWebView(QtWebKit.QWebView): 
    _windows = set() 

    def __init__(self, parent=None): 
     super(myWebView, self).__init__(parent) 
     self.settings().setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True) 
     self.settings().setAttribute(QtWebKit.QWebSettings.JavascriptCanOpenWindows, True) 

     self.page().setNetworkAccessManager(networkAccessManager) 

     self.load(QtCore.QUrl("http://www.example.com")) 

    @classmethod 
    def _removeWindow(cls, window): 
     if window in cls._windows: 
      cls._windows.remove(window) 

    @classmethod 
    def newWindow(cls): 
     window = cls() 
     cls._windows.add(window) 
     return window 

    def closeEvent(self, event): 
     self._removeWindow(self) 
     event.accept() 

    def createWindow(self, webWindowType): 
     window = self.newWindow() 
     if webWindowType == QtWebKit.QWebPage.WebModalDialog: 
      window.setWindowModality(QtCore.Qt.ApplicationModal) 

     window.show() 

     return window 

class myWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     super(myWindow, self).__init__(parent) 

     self.centralwidget = QtGui.QWidget(self) 

     self.webView = myWebView(self.centralwidget) 

     self.pushButton = QtGui.QPushButton(self.centralwidget) 
     self.pushButton.setText("New Window") 
     self.pushButton.clicked.connect(lambda: self.webView.createWindow(QtWebKit.QWebPage.WebBrowserWindow)) 

     self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget) 
     self.verticalLayout.addWidget(self.pushButton) 
     self.verticalLayout.addWidget(self.webView) 

     self.setCentralWidget(self.centralwidget) 

if __name__ == "__main__": 
    import sys 

    app = QtGui.QApplication(sys.argv) 
    main = myWindow() 
    main.show() 
    sys.exit(app.exec_()) 
+0

+1感謝的人,工作的一種享受。 – jdborg