问题
How do I get a link's URL when clicking on the link?
If I have a link in the A tag, it's simple: just connect linkClicked(const QUrl&)
signal to specific slot.
But if I have a table with an "onClick" event on its cell (generated html: "<td onClick=\"window.location.href='" + link_ + "';\"
......blahblahblah"), it's not working. Why?
回答1:
As it's name suggests, the linkClicked signal is only emitted whenever a link is activated.
But you can intercept all navigation requests by reimplementing acceptNavigationRequest:
class WebPage(QtWebKit.QWebPage):
def __init__(self, parent=None):
super(WebPage, self).__init__(parent)
self.setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateExternalLinks)
def acceptNavigationRequest(self, frame, request, type):
print('Navigation Request:', request.url())
return False
...
webview.setPage(WebPage())
But note that all navigation requests are passed through this method, so your implementation should return True
whenever it does not intend to handle the request.
来源:https://stackoverflow.com/questions/27604858/how-to-get-link-url-on-onclick-in-a-qwebview