默认的qwebview,点击图片等链接,是不能直接打开的,我查阅了一下,原来是有些配置没有指定而导致的,具体设置如下:
#coding=utf-8 __author__ = 'Administrator' from PyQt5.QtWebKit import * from PyQt5.QtWidgets import * from PyQt5.QtWebKitWidgets import * from PyQt5.QtCore import * from PyQt5.QtNetwork import * import sys def setAppProxy(): proxy = QNetworkProxy() #Http访问代理 proxy.setType(QNetworkProxy.HttpProxy) #proxy.setType(QtNetwork.QNetworkProxy.DefaultProxy) proxy.setHostName(u'proxy3.bj.petrochina') proxy.setPort(8080) QNetworkProxy.setApplicationProxy(proxy) app = QApplication(sys.argv) setAppProxy() #使用系统的设置,如果有代理,则自动使用代理 #QNetworkProxyFactory.setUseSystemConfiguration(True) wv = QWebView() setting = wv.settings() ''' setting.setAttribute(QWebSettings.JavascriptCanOpenWindows, True) setting.setAttribute(QWebSettings.JavascriptCanCloseWindows, True) setting.setAttribute(QWebSettings.JavascriptCanAccessClipboard, True) setting.setAttribute(QWebSettings.SpatialNavigationEnabled, True) wv.page().action(QWebPage.OpenLinkInNewWindow).setEnabled(True) ''' #设置链接的代理默认,对所有的链接都处理,不设置则图片链接点击不起作用 wv.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) #设置链接的处理方式,可以自定制例如处理本地的链接,不设置点击图片链接不起作用 wv.linkClicked.connect(wv.load) wv.show() wv.load(QUrl('https://www.baidu.com/')) app.exec_()
stackoverflow上的解决方案:
http://stackoverflow.com/questions/6951199/qwebview-doesnt-open-links-in-new-window-and-not-start-external-application-for