QWebView 自动选项卡

发布于 2024-10-13 07:39:30 字数 522 浏览 12 评论 0原文

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

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

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

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

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

感谢您的任何想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2024-10-20 07:39:30

根据我的理解,这是开发人员的工作,为单击的网址创建和打开新选项卡。您需要为 QWebView::linkClicked 信号。每当用户单击链接并且页面的 linkDelegationPolicy 属性设置为委托指定 url 的链接处理时,就会发出此信号。您可以在其中创建 QWebView 的新实例,为其添加选项卡并在其中打开新的 url。下面是一个例子:

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()

希望这有帮助,问候

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文