【问题标题】:QWebEngineView crashes when accessing specific websitesQWebEngineView 在访问特定网站时崩溃
【发布时间】:2021-08-11 21:13:53
【问题描述】:

我有一个非常奇怪的 QWebEngineView 错误。下面是一些创建 QWebEngineView 的代码。

import sys

from PyQt5.QtCore import QCoreApplication, QFileInfo, QUrl
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings, QWebEngineProfile, QWebEnginePage

class Widget(QWidget):
    def __init__(self):
        super().__init__()
        webview = QWebEngineView(self)
        webview.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        profile = QWebEngineProfile("my_profile", webview)
        webpage = QWebEnginePage(profile, webview)
        webview.setUrl(QUrl("https://www.youtube.com"))
        #self.webview.setPage(self.webpage)
        webview.setGeometry(0, 0, 800, 480)
        webview.show()

        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    app.exec_()

我的问题是,当我按原样运行代码时(self.webview.setPage(self.webpage) 行被注释掉)我能够打开 PDF 文件,但是打开 YouTube 会使程序崩溃。但是,如果我取消注释并运行它,我将无法打开 PDF 文件(虽然它不会使程序崩溃,但它只是无法打开 PDF 文件),但我可以毫无问题地打开 YouTube。

【问题讨论】:

  • @eyllanesc 我更改了问题中的代码

标签: python pyqt5 qwebengineview qwebenginepage


【解决方案1】:

你必须先设置 QWebEngineProfile 和 QWebEnginePage 然后启用插件:

import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import (
    QWebEnginePage,
    QWebEngineProfile,
    QWebEngineSettings,
    QWebEngineView,
)


class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        profile = QWebEngineProfile("my_profile", self)
        webpage = QWebEnginePage(profile, self)
        webview = QWebEngineView(self)
        webview.setPage(webpage)
        webview.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        webview.load(QUrl("https://www.youtube.com"))

        lay = QVBoxLayout(self)
        lay.addWidget(webview)
        self.resize(640, 480)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = Widget()
    w.show()
    app.exec_()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-27
    • 2019-10-19
    • 2013-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多