QWebView 自动标签 [英] QWebView Auto Tabs

查看:30
本文介绍了QWebView 自动标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 QWebView.load(QUrl(myurl)) 打开一个网页,网页得到一些输入并返回一个新的 php 生成页面.

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

如果在 Firefox 中执行,浏览器会自动打开一个新标签/窗口以显示返回的页面.

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

如何告诉 QWebView 打开一个新的 QWebview 实例并加载返回的数据?

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

我正在查看 QwebView 文档www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qwebview.html ...但没有乐趣.

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

此类页面的示例:http://www.iqfront.com/index.php?option=com_content&view=article&id=5&Itemid=4

感谢您的任何想法.

推荐答案

据我所知,这是开发人员的工作,即为点击的 url 创建和打开新选项卡.您需要为 QWebView::linkClicked 信号.每当用户单击链接并且页面的 linkDelegationPolicy 属性设置为委托指定 url 的链接处理时,就会发出此信号.在那里你可以创建一个 QWebView 的新实例,将它添加到一个选项卡并在那里打开新的 url.下面是一个例子:

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

这篇关于QWebView 自动标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆