简体   繁体   English

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

[英]How to enable incognito mode in PyQtWebEngine?

I am making a web browser using PyQtWebEngine but how will I give the feature of incognito mode in it.我正在使用 PyQtWebEngine 制作 web 浏览器,但我将如何在其中提供隐身模式的功能。

The answer is in the example that I already pointed out in a previous post: WebEngine Widgets Simple Browser Example .答案就在我在上一篇文章中已经指出的示例中: 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() since the latter is shared by all pages by default, which is what is not searched for in a private browsing.实现私人浏览部分,他们指出提供与QWebEngineProfile::defaultProfile() QWebEngineProfile()就足够了,因为后者默认由所有页面共享,这是在私人浏览中不搜索的内容。

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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