简体   繁体   中英

How to set a QWebEngineProfile to a QWebEngineView

I want to set different QWebEngineProfiles to different QWebEngineViews meaning each view will have its own cookie store. I cant find any documentation on it therefore all help would be greatly appreciated. Any suggestion of another method of setting independent cookie stores to independent webviews will also help. Cheers.

Code is below (connecting the signal did not format properly here but rest assured it is correct in the real code):

from PyQt5.QtCore import *
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args,**kwargs)
        self.browser={}
        self.cookiestore={}
        self.page={}
        No = input("No: ")
        for i in range(int(No)):
            self.browser[str(i)] = QWebEngineView()
            storagename = str(i)
            self.cookiestore[str(i)] = QWebEngineProfile(storagename, self.browser[str(i)])
            self.page[str(i)] = QWebEnginePage(self.cookiestore[str(i)], self.browser[str(i)])
            self.browser[str(i)].setPage(self.page[str(i)])
            self.browser[str(i)].load(QUrl("https://www.google.com"))
      self.browser[str(i)].loadFinished.connect(lambda:self._loaded(str(i)))

    def _loaded(self, No):
        self.browser[No].page().toHtml(self._callable)
    def _callable(self, data):
        self.html = data
        if "" in self.html:
            print("Done")
        else:
            print("wait")

app = QApplication(sys.argv)
window = MainWindow()
app.exec_()

If you want to establish a QWebEngineProfile to a QWebEngineView you must do it through a QWebEnginePage as I show below:

webview = QWebEngineView()
profile = QWebEngineProfile("somestorage", webview)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)

Example:

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
from PyQt5.QtWidgets import QApplication

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    views = []
    for i in range(5):
        webview = QWebEngineView()
        profile = QWebEngineProfile(f"storage-{i}", webview)
        webpage = QWebEnginePage(profile, webview)
        webview.setPage(webpage)
        webview.load(QUrl("https://stackoverflow.com/questions/48142341/how-to-set-a-qwebengineprofile-to-a-qwebengineview"))
        webview.show()
        views.append(webview)
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM