这是错误。
QWebView is not defined
我正在通过https://wiki.python.org/moin/PyQt/Embedding%20Widgets%20in%20Web%20Pages的PyQt4教程进行学习。
我了解定义,但是我应该在这里定义什么,在哪里定义?
我将在哪里安装QWebView?
我知道所提供的链接是一个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属于最后一个子模块:
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(社区维护了一个fork),因为它已被QtWebEngine取代,因此要使用QtWebkit,必须使用每个发行版的官方存储库或手动进行编译。QtWebEngine不能实现QWebPluginFactory的功能,因为渲染不是由Qt而是由铬进行的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句