sudo apt install python3-pyqt5 等apt安装方式

sudo apt install python3-pyqt5 and other apt install methods

这里是错误。

QWebView is not defined

我正在关注 https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages 上的 PyQt4 教程。

我理解定义,但我到底应该在这里定义什么,我应该在哪里定义它?

我应该在哪里安装 QWebView?

我知道 link 提供的是一个 PyQt4 示例,我正在使用 PyQt5。我知道有变化,但我现在还没有找到确切的答案。

这是一些代码:

import sys
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import *
from PyQt5.QtWebKit import *
from PyQt5.QtWidgets import *

html = \
"""<html>
<head>
<title>Python Web Plugin Test</title>
</head>

<body>
<h1>Python Web Plugin Test</h1>
<object type="x-pyqt/widget" width="200" height="200"></object>
<p>This is a Web plugin written in Python.</p>
</body>
</html>
"""

class WebWidget(QWidget):

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        painter.setBrush(Qt.white)
        painter.setPen(Qt.black)
        painter.drawRect(self.rect().adjusted(0, 0, -1, -1))
        painter.setBrush(Qt.red)
        painter.setPen(Qt.NoPen)
        painter.drawRect(self.width()/4, self.height()/4,
                         self.width()/2, self.height()/2)
        painter.end()

    def sizeHint(self):
        return QSize(100, 100)

class WebPluginFactory(QWebPluginFactory):

    def __init__(self, parent = None):
        QWebPluginFactory.__init__(self, parent)

    def create(self, mimeType, url, names, values):
        if mimeType == "x-pyqt/widget":
            return WebWidget()

    def plugins(self):
        plugin = QWebPluginFactory.Plugin()
        plugin.name = "PyQt Widget"
        plugin.description = "An example Web plugin written with PyQt."
        mimeType = QWebPluginFactory.MimeType()
        mimeType.name = "x-pyqt/widget"
        mimeType.description = "PyQt widget"
        mimeType.fileExtensions = []
        plugin.mimeTypes = [mimeType]
        print("plugins")
        return [plugin]

if __name__ == "__main__":

    app = QApplication(sys.argv)
    QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
    view = QWebView()
    factory = WebPluginFactory()
    view.page().setPluginFactory(factory)
    view.setHtml(html)
    view.show()
    sys.exit(app.exec_())

要使用 QtWebkit,您必须安装包 python3-pyqt5.qtwebkit。另一方面,从 Qt4 到 Qt5 的主要变化是模块的重组,这发生在 QtWebKit 分为 QtWebKit 和 QtWebkitWidgets,所以 QWebView 属于最后 sub-module:

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWebKit import QWebPluginFactory, QWebSettings
from PyQt5.QtWebKitWidgets import QWebView

html = """<html>
<head>
<title>Python Web Plugin Test</title>
</head>

<body>
<h1>Python Web Plugin Test</h1>
<object type="x-pyqt/widget" width="200" height="200"></object>
<p>This is a Web plugin written in Python.</p>
</body>
</html>
"""


class WebWidget(QWidget):
    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        painter.setBrush(Qt.white)
        painter.setPen(Qt.black)
        painter.drawRect(self.rect().adjusted(0, 0, -1, -1))
        painter.setBrush(Qt.red)
        painter.setPen(Qt.NoPen)
        painter.drawRect(
            self.width() / 4, self.height() / 4, self.width() / 2, self.height() / 2
        )
        painter.end()

    def sizeHint(self):
        return QSize(100, 100)


class WebPluginFactory(QWebPluginFactory):
    def __init__(self, parent=None):
        QWebPluginFactory.__init__(self, parent)

    def create(self, mimeType, url, names, values):
        if mimeType == "x-pyqt/widget":
            return WebWidget()

    def plugins(self):
        plugin = QWebPluginFactory.Plugin()
        plugin.name = "PyQt Widget"
        plugin.description = "An example Web plugin written with PyQt."
        mimeType = QWebPluginFactory.MimeType()
        mimeType.name = "x-pyqt/widget"
        mimeType.description = "PyQt widget"
        mimeType.fileExtensions = []
        plugin.mimeTypes = [mimeType]
        print("plugins")
        return [plugin]


if __name__ == "__main__":

    app = QApplication(sys.argv)
    QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
    view = QWebView()
    factory = WebPluginFactory()
    view.page().setPluginFactory(factory)
    view.setHtml(html)
    view.show()
    sys.exit(app.exec_())

注意:由于 Qt 5.6 QtWebkit 不再由官方维护(社区维护了一个分支),因为它已被 QtWebEngine 取代,因此要使用 QtWebkit,您必须使用每个发行版的官方存储库或手动编译。 QWebPluginFactory 的功能无法由 QtWebEngine 实现,因为渲染不是由 Qt 而是由 Chromium 完成的。