我正在开发一个用户必须先登录的应用程序。我正在使用 QWebEngineView 来显示登录页面。但是在其中一台机器中,该页面没有显示。我想知道是否有任何 SSL 错误。如何获取 sslerrors 信号并将其连接到插槽。下面的示例代码
from PyQt5.QtNetwork import QSslConfiguration, QSsl
from PyQt5.QtWidgets import QWidget, QGridLayout, QStatusBar, QApplication,\
QMainWindow
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QCoreApplication, QMetaObject, QUrl
def set_ssl_protocol():
default_config = QSslConfiguration.defaultConfiguration()
default_config.setProtocol(QSsl.TlsV1_2)
QSslConfiguration.setDefaultConfiguration(default_config)
set_ssl_protocol()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.urlString = "https://www.yahoo.com"
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.webView = QWebEngineView(self.centralwidget)
self.webView=QWebEngineView()
self.webView.setUrl(QUrl("about:blank"))
# self.webView.setUrl(QUrl("http://www.google.com/"))
self.webView.setObjectName("webView")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.loadURL()
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
def loadURL(self):
if not hasattr(self, 'page'):
self.page = QWebEnginePage()
self.webView.setPage(self.page)
self.page.load(QUrl(self.urlString))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
注意:在 Qt4 中这很容易,因为我们可以将 page().networkAccessManager().sslerrors 信号连接到插槽,而在 Qt5 中,由于请求不是通过 NetworkAccessManager 发出的,因此调试起来有点困难。