Linux rwilsonaspaspe5 -521 3.16.0-47-generic #63~14.04.1-Ubuntu SMP星期五Aug 14 22:56:59 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux <<p> PyQt版本/strong>
('Qt version:', '4.8.6')
('SIP version:', '4.15.5')
('PyQt version:', '4.10.4')
我正在使用PyQt4/QtWebKit的python web浏览器上工作,我需要启用插件,以便它可以渲染flash内容。
当我添加这一行时,我得到一个核心转储QWebSettings. globalsettings (). setattribute (QWebSettings.)。PluginsEnabled,真的)
Linux rwilsonaspaspe5 -521 3.16.0-47-generic #63~14.04.1-Ubuntu SMP星期五Aug 14 22:56:59 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux <<p> PyQt版本/strong>
('Qt version:', '4.8.6')
('SIP version:', '4.15.5')
('PyQt version:', '4.10.4')
以下是所有代码
#!/usr/bin/env python
from PyQt4 import QtCore, QtGui, QtNetwork, QtWebKit
from PyQt4.QtWebKit import QWebSettings
QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
try:
import jquery_rc3
except ImportError:
import jquery_rc2
class MainWindow(QtGui.QMainWindow):
def __init__(self, url):
super(MainWindow, self).__init__()
self.progress = 0
fd = QtCore.QFile(":/jquery.min.js")
if fd.open(QtCore.QIODevice.ReadOnly | QtCore.QFile.Text):
self.jQuery = QtCore.QTextStream(fd).readAll()
fd.close()
else:
self.jQuery = ''
QtNetwork.QNetworkProxyFactory.setUseSystemConfiguration(True)
self.view = QtWebKit.QWebView(self)
self.view.load(url)
self.view.loadFinished.connect(self.adjustLocation)
self.view.titleChanged.connect(self.adjustTitle)
self.view.loadProgress.connect(self.setProgress)
self.view.loadFinished.connect(self.finishLoading)
self.locationEdit = QtGui.QLineEdit(self)
self.locationEdit.setSizePolicy(QtGui.QSizePolicy.Expanding,
self.locationEdit.sizePolicy().verticalPolicy())
self.locationEdit.returnPressed.connect(self.changeLocation)
toolBar = self.addToolBar("Navigation")
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Back))
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Forward))
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Reload))
toolBar.addAction(self.view.pageAction(QtWebKit.QWebPage.Stop))
toolBar.addWidget(self.locationEdit)
viewMenu = self.menuBar().addMenu("&View")
viewSourceAction = QtGui.QAction("Page Source", self)
viewSourceAction.triggered.connect(self.viewSource)
viewMenu.addAction(viewSourceAction)
effectMenu = self.menuBar().addMenu("&Effect")
effectMenu.addAction("Highlight all links", self.highlightAllLinks)
self.rotateAction = QtGui.QAction(
self.style().standardIcon(
QtGui.QStyle.SP_FileDialogDetailedView),
"Turn images upside down", self, checkable=True,
toggled=self.rotateImages)
effectMenu.addAction(self.rotateAction)
toolsMenu = self.menuBar().addMenu("&Tools")
toolsMenu.addAction("Remove GIF images", self.removeGifImages)
toolsMenu.addAction("Remove all inline frames",
self.removeInlineFrames)
toolsMenu.addAction("Remove all object elements",
self.removeObjectElements)
toolsMenu.addAction("Remove all embedded elements",
self.removeEmbeddedElements)
self.setCentralWidget(self.view)
self.setUnifiedTitleAndToolBarOnMac(True)
def viewSource(self):
accessManager = self.view.page().networkAccessManager()
request = QtNetwork.QNetworkRequest(self.view.url())
reply = accessManager.get(request)
reply.finished.connect(self.slotSourceDownloaded)
def slotSourceDownloaded(self):
reply = self.sender()
self.textEdit = QtGui.QTextEdit(None)
self.textEdit.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.textEdit.show()
self.textEdit.setPlainText(QtCore.QTextStream(reply).readAll())
self.textEdit.resize(600, 400)
reply.deleteLater()
def adjustLocation(self):
self.locationEdit.setText(self.view.url().toString())
def changeLocation(self):
url = QtCore.QUrl.fromUserInput(self.locationEdit.text())
self.view.load(url)
self.view.setFocus()
def adjustTitle(self):
if 0 < self.progress < 100:
self.setWindowTitle("%s (%s%%)" % (self.view.title(), self.progress))
else:
self.setWindowTitle(self.view.title())
def setProgress(self, p):
self.progress = p
self.adjustTitle()
def finishLoading(self):
self.progress = 100
self.adjustTitle()
self.view.page().mainFrame().evaluateJavaScript(self.jQuery)
self.rotateImages(self.rotateAction.isChecked())
def highlightAllLinks(self):
code = """$('a').each(
function () {
$(this).css('background-color', 'yellow')
}
)"""
self.view.page().mainFrame().evaluateJavaScript(code)
def rotateImages(self, invert):
if invert:
code = """
$('img').each(
function () {
$(this).css('-webkit-transition', '-webkit-transform 2s');
$(this).css('-webkit-transform', 'rotate(180deg)')
}
)"""
else:
code = """
$('img').each(
function () {
$(this).css('-webkit-transition', '-webkit-transform 2s');
$(this).css('-webkit-transform', 'rotate(0deg)')
}
)"""
self.view.page().mainFrame().evaluateJavaScript(code)
def removeGifImages(self):
code = "$('[src*=gif]').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)
def removeInlineFrames(self):
code = "$('iframe').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)
def removeObjectElements(self):
code = "$('object').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)
def removeEmbeddedElements(self):
code = "$('embed').remove()"
self.view.page().mainFrame().evaluateJavaScript(code)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
if len(sys.argv) > 1:
url = QtCore.QUrl(sys.argv[1])
else:
url = QtCore.QUrl('http://www.google.com/ncr')
browser = MainWindow(url)
browser.show()
sys.exit(app.exec_())
在使用QWebSettings
之前,您需要有QApplication
的实例
尝试将违规行移动到MainWindow.__init__