使用 PySide 覆盖 QWebPage 中的 shouldInterruptJavaScript
我想重写 PySide.QtWebKit.QWebPage.shouldInterruptJavaScript() 槽以静默忽略 JavaScript 中断请求。我有自己的超时计时器设置,不需要默认消息对话框。
引用PySide 文档:
由于二进制兼容性限制,此函数不是虚拟的。如果您想在 PySide.QtWebKit.QWebPage 子类中提供自己的实现,请在您的子类中重新实现 QWebPage.shouldInterruptJavaScript() 插槽。 QtWebKit 会动态检测槽并调用它。
这是我编写的示例代码,但我的 shouldInterruptJavaScript() 方法从未被调用。我看到 PhantomJS 和 webscraping 开源项目中使用了相同的代码。
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_())
我有Python 2.7.1、PySide 1.0.2、Qt 4.7.2。现在我正在构建最新的 PySide,以便我可以测试它,但我在最近的发行说明或错误报告中找不到有关 shouldInterruptJavaScript 的任何内容。
有什么特别的地方我应该如何在我的子类中重新实现shouldInterruptJavaScript?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,在 1.0.6 版本发布后,这个问题已在 PySide git master 上修复。
This is now fixed on PySide git master after the release 1.0.6.