子类化的 QWebView 不会对超链接点击做出反应
这是在 Python/PySide 中。
我试图通过重载 PySide.QtWebKit.QWebView 小部件来创建自己的家长 WebBrowser。然后,每当有人单击小部件上的链接时,我都会检查是否会访问无效网站,如果不是,我们将继续,如果是,则我会重定向到通用页面。
所以我已经对 PySide.QtWebKit.QWebView 进行了子类化,但我没有收到单击链接时的通知。我已经重写了 linkClicked 函数,但单击链接时该函数永远不会运行?
我做错了什么?为什么我的函数无法运行/对超链接单击“事件”做出反应?我是否需要覆盖网页对象&这个类不是对链接点击做出反应的吗?
import PySide.QtWebKit
import sys
from PyQt4 import QtGui
class BrowserWindow( PySide.QtWebKit.QWebView ):
# Class Variables:
def __init__( self, _parent ):
""" Constructor: """
super(BrowserWindow, self).__init__()
PySide.QtWebKit.QWebView(None)
def linkClicked(self, arg__1):
""" Post: """
#print("LINK CLICKED")
#text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
# 'Enter your name:')
self.load("http://yahoo.com")
def main():
app = QtGui.QApplication(sys.argv)
view = BrowserWindow(None) #PySide.QtWebKit.QWebView(None)
view.load("http://google.com")
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
This is in Python/PySide.
I am trying to create my own Parental WebBrowser by overloading the PySide.QtWebKit.QWebView widget. Then whenever someone clicks a link on the widget I check to see if we are going to an invalid website, if not we proceed, if yes then I redirect to a generic page.
So I have subclassed the PySide.QtWebKit.QWebView, but I am not receiving notification of when a link is clicked. I have overridden the linkClicked function but the function never runs when a link is clicked?
What am I doing wrong? Why cant my function run/react to the hyperlink click "event"? Do I need to override the webpage object & not this class to react to link clicks?
import PySide.QtWebKit
import sys
from PyQt4 import QtGui
class BrowserWindow( PySide.QtWebKit.QWebView ):
# Class Variables:
def __init__( self, _parent ):
""" Constructor: """
super(BrowserWindow, self).__init__()
PySide.QtWebKit.QWebView(None)
def linkClicked(self, arg__1):
""" Post: """
#print("LINK CLICKED")
#text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
# 'Enter your name:')
self.load("http://yahoo.com")
def main():
app = QtGui.QApplication(sys.argv)
view = BrowserWindow(None) #PySide.QtWebKit.QWebView(None)
view.load("http://google.com")
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的代码有几个问题。首先,您要导入 PySide 和 PyQt4,这不是一个好主意。其次,
QWebView.linkClicked
是一个信号,而不是受保护的方法,因此您无法覆盖它。第三,当您应该传递QtCore.QUrl
时,您正在将字符串传递给QWebView.load
。但是,除了这些问题之外,您还需要设置linkDelegationPolicy 在网页上,以覆盖其链接处理。
这是代码的编辑版本,应该可以解决所有问题:
There are several problems with the code you posted. Firstly, you are importing both PySide and PyQt4, which is not a good idea. Secondly,
QWebView.linkClicked
is a signal, not a protected method, so you can't override it. Thirdly, you are passing a string toQWebView.load
, when you should be passing aQtCore.QUrl
.However, aside from those problems, you also need to set the linkDelegationPolicy on the web page in order to override its link handling.
Here's an edited version of your code which should fix all the problems: