如何在 python 和 QtWebEngine 中启用 allowGeolocationOnInsecureOrigins

How to enable allowGeolocationOnInsecureOrigins in python and QtWebEngine

我正在 Python 和 PyQt5 中制作网络浏览器,我想在我的网络浏览器中启用 allowGeolocationOnInsecureOrigins 以便我可以通过 Google 地理位置 API 访问用户位置。

代码

self.browser = QWebEngineView()
self.browser.allowGeolocationOnInsecureOrigins(1) 

错误

self.browser.allowGeolocationOnInsecureOrigins(1)
AttributeError: 'QWebEngineView' object has no attribute 'allowGeolocationOnInsecureOrigins'

enum QWebEngineSettings::WebAttribute

QWebEngineSettings::AllowWindowActivationFromJavaScript Since Qt 5.7, only secure origins such as HTTPS have been able to request Geolocation features. This provides an override to allow non secure origins to access Geolocation again. Disabled by default. (Added in Qt 5.9)

import sys
from PyQt5.QtCore    import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings

app = QApplication(sys.argv)

browser = QWebEngineView()
browser.page().settings().setAttribute(                            # <---
    QWebEngineSettings.AllowGeolocationOnInsecureOrigins, True)    # <---

browser.load(QUrl("https://doc.qt.io/qt-5/qwebenginesettings.html#WebAttribute-enum"))
browser.show()

sys.exit(app.exec_())