QWebView Auto Tabs

2019-04-14 03:11发布

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条回答
别忘想泡老子
2楼-- · 2019-04-14 04:07

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

查看更多
登录 后发表回答