PyQt5 / PyQt

PyQt Examples(PyQt各种测试和例子) PyQt4 PyQt5
GNU Lesser General Public License v2.1
6.65k stars 1.97k forks source link

Release of profile requested but WebEnginePage still not deleted. Expect troubles ! #158

Closed godomainz closed 1 year ago

godomainz commented 2 years ago

I have a python code like below

import sys
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtCore import QUrl, QTimer
from PyQt5.QtGui import QPageLayout, QPageSize
from PyQt5.QtWidgets import QApplication
import argparse

def main(app):
    url = ''
    parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("--url", help="Type url")
    args = parser.parse_args()
    config = vars(args)
    url = config['url']

    loader = QtWebEngineWidgets.QWebEngineView()
    profile = QtWebEngineWidgets.QWebEngineProfile("storage", loader)
    cookie_store = profile.cookieStore()

    with open('cookie.txt', 'rb') as f:
        contents = f.read()

    cookie_store.setCookie(QNetworkCookie(contents))
    webpage = QtWebEngineWidgets.QWebEnginePage(profile, loader)
    COOKIPATH = "./Cache" + str(1)
    # web_profile.setCachePath(COOKIPATH)
    profile.setPersistentStoragePath(COOKIPATH)
    profile.setPersistentCookiesPolicy(2)
    loader.setPage(webpage)
    loader.setZoomFactor(1)
    layout = QPageLayout()
    layout.setPageSize(QPageSize(QPageSize.A4Extra))
    layout.setOrientation(QPageLayout.Portrait)
    loader.load(QUrl(url))
    loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())

    def emit_pdf(finished):
        QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout))

    loader.loadFinished.connect(emit_pdf)
    app.exec()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main(app)

I run above code like this python3 htmlToPdfnew.py --url http://example.com

but when I run the code I get a warning like below

Release of profile requested but WebEnginePage still not deleted. Expect troubles ! based on suggestions I got from internet calling app.exec()

but still get that error

how to resolve that error ?

892768447 commented 1 year ago

https://github.com/PyQt5/PyQt/blob/master/QWebEngineView/SiteDiffUser.py