QWebView Auto Tabs

这一生的挚爱 提交于 2019-12-06 10:27:46

问题


I open a web page with QWebView.load(QUrl(myurl)) , the webpage gets some input and returns a new php generated page.

If executed in Firefox the browser automatically opens a new tab/window to show the returned page.

How to tell QWebView to open a new instance of QWebview with the returned data loaded?

I was looking at at the QwebView documentation at www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwebview.html ... but no joy.

Example of such a page : http://www.iqfront.com/index.php?option=com_content&view=article&id=5&Itemid=4

Thanks for any ideas.


回答1:


from my understanding this is developer's job to create and open new tabs for urls clicked.You would need to define a custom slot for QWebView::linkClicked signal. This signal is emitted whenever the user clicks on a link and the page's linkDelegationPolicy property is set to delegate the link handling for the specified url. There you can create a new instance of QWebView add it a tab and open new url there. Below is an example:

import sys
from PyQt4 import QtGui, QtCore, QtWebKit 

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.tabWidget = QtGui.QTabWidget(self)        
        self.setCentralWidget(self.tabWidget)        
        self.loadUrl(QtCore.QUrl('http://qt.nokia.com/'))

    def loadUrl(self, url):    
        view = QtWebKit.QWebView()  
        view.connect(view, QtCore.SIGNAL('loadFinished(bool)'), self.loadFinished)
        view.connect(view, QtCore.SIGNAL('linkClicked(const QUrl&)'), self.linkClicked)
        view.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateAllLinks)
        self.tabWidget.setCurrentIndex(self.tabWidget.addTab(view, 'loading...'))
        view.load(url)

    def loadFinished(self, ok):
        index = self.tabWidget.indexOf(self.sender())
        self.tabWidget.setTabText(index, self.sender().url().host())

    def linkClicked(self, url):        
        self.loadUrl(url)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

hope this helps, regards



来源:https://stackoverflow.com/questions/4766186/qwebview-auto-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!