问题
I would like to override PySide.QtWebKit.QWebPage.shouldInterruptJavaScript() slot to silently ignore JavaScript interrupt requests. I have my own timeout timer setup and I do not need a default message dialog.
Quoting PySide documentation:
Because of binary compatibility constraints, this function is not virtual. If you want to provide your own implementation in a PySide.QtWebKit.QWebPage subclass, reimplement the QWebPage.shouldInterruptJavaScript() slot in your subclass instead. QtWebKit will dynamically detect the slot and call it.
This is an example code I wrote, but my shouldInterruptJavaScript() method is never called. I see the same code used in PhantomJS and webscraping open source projects.
import sys
from PySide import QtCore
from PySide.QtGui import QApplication
from PySide.QtWebKit import QWebPage
class QWebPageHeadless(QWebPage):
# FIXME: This is not working, the slot is not overriden!
@QtCore.Slot()
def shouldInterruptJavaScript(self):
print "Interrupt javascript request ignored..."
return False
if __name__ == "__main__":
app = QApplication(sys.argv)
page = QWebPageHeadless()
page.mainFrame().setHtml('<script>while(1);</script>')
sys.exit(app.exec_())
I have Python 2.7.1, PySide 1.0.2, Qt 4.7.2. Right now I am building the latest PySide so I can test it, but I can not find anything about shouldInterruptJavaScript in recent release notes or bug reports.
Is there something special how should I reimplement shouldInterruptJavaScript in my subclass?
回答1:
This is now fixed on PySide git master after the release 1.0.6.
来源:https://stackoverflow.com/questions/6868286/override-shouldinterruptjavascript-in-qwebpage-with-pyside