如何在 PyQtWebEngine 中启用隐身模式?

How to enable incognito mode in PyQtWebEngine?

我正在使用 PyQtWebEngine 制作一个网络浏览器,但我将如何在其中提供隐身模式的功能。

答案在我之前已经指出的例子中 post: WebEngine Widgets Simple Browser Example. In the Implementing Private Browsing section they point out that it is enough to provide a QWebEngineProfile() different from QWebEngineProfile::defaultProfile() 因为后者默认被所有页面共享,这是在私密浏览。

from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets


class WebView(QtWebEngineWidgets.QWebEngineView):
    def __init__(self, off_the_record=False, parent=None):
        super().__init__(parent)
        profile = (
            QtWebEngineWidgets.QWebEngineProfile()
            if off_the_record
            else QtWebEngineWidgets.QWebEngineProfile.defaultProfile()
        )
        page = QtWebEngineWidgets.QWebEnginePage(profile)
        self.setPage(page)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    view = WebView(off_the_record=True)

    view.load(QtCore.QUrl("https://www.qt.io"))
    view.show()

    sys.exit(app.exec_())