<bdo id='YZ5UW'></bdo><ul id='YZ5UW'></ul>

    <legend id='YZ5UW'><style id='YZ5UW'><dir id='YZ5UW'><q id='YZ5UW'></q></dir></style></legend>
    1. <tfoot id='YZ5UW'></tfoot>

      <small id='YZ5UW'></small><noframes id='YZ5UW'>

      1. <i id='YZ5UW'><tr id='YZ5UW'><dt id='YZ5UW'><q id='YZ5UW'><span id='YZ5UW'><b id='YZ5UW'><form id='YZ5UW'><ins id='YZ5UW'></ins><ul id='YZ5UW'></ul><sub id='YZ5UW'></sub></form><legend id='YZ5UW'></legend><bdo id='YZ5UW'><pre id='YZ5UW'><center id='YZ5UW'></center></pre></bdo></b><th id='YZ5UW'></th></span></q></dt></tr></i><div id='YZ5UW'><tfoot id='YZ5UW'></tfoot><dl id='YZ5UW'><fieldset id='YZ5UW'></fieldset></dl></div>

        窗口未在外部 url 链接上打开新窗口或选项卡单击

        时间:2023-08-06

        <small id='j9rq4'></small><noframes id='j9rq4'>

          <tbody id='j9rq4'></tbody>
        • <legend id='j9rq4'><style id='j9rq4'><dir id='j9rq4'><q id='j9rq4'></q></dir></style></legend>
            <bdo id='j9rq4'></bdo><ul id='j9rq4'></ul>

                <tfoot id='j9rq4'></tfoot>
              1. <i id='j9rq4'><tr id='j9rq4'><dt id='j9rq4'><q id='j9rq4'><span id='j9rq4'><b id='j9rq4'><form id='j9rq4'><ins id='j9rq4'></ins><ul id='j9rq4'></ul><sub id='j9rq4'></sub></form><legend id='j9rq4'></legend><bdo id='j9rq4'><pre id='j9rq4'><center id='j9rq4'></center></pre></bdo></b><th id='j9rq4'></th></span></q></dt></tr></i><div id='j9rq4'><tfoot id='j9rq4'></tfoot><dl id='j9rq4'><fieldset id='j9rq4'></fieldset></dl></div>

                • 本文介绍了窗口未在外部 url 链接上打开新窗口或选项卡单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要使用 pyQt5 打开一个 URL.该页面有几个链接可以打开一个新窗口.pyQt5 为 URL 打开一个窗口,但在单击应该打开一个新窗口的链接后不执行任何操作.PS我正在使用pyQt5.6

                  I need to open an URL using pyQt5. The page has several links that open a new window. pyQt5 opens a windows for the URL but does not do anything after clicking on a link that should open a new window. P.S I'm using pyQt5.6

                  我已经在 Linux centOs 上尝试过,但没有任何效果.

                  I have tried it on Linux centOs but nothing works.

                  from PyQt5.QtCore import QUrl
                  from PyQt5.QtGui import QDesktopServices
                  from PyQt5.QtWidgets import QApplication
                  from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
                  
                  
                  class WebEnginePage(QWebEnginePage):
                      def acceptNavigationRequest(self, url,  _type, isMainFrame):
                          if _type == QWebEnginePage.NavigationTypeLinkClicked:
                              return True
                          return QWebEnginePage.acceptNavigationRequest(self, url,  _type,      isMainFrame)
                  
                  class HtmlView(QWebEngineView):
                      def __init__(self, *args, **kwargs):
                          QWebEngineView.__init__(self, *args, **kwargs)
                          self.setPage(WebEnginePage(self))
                  
                  if __name__ == '__main__':
                      import sys
                  
                      app = QApplication(sys.argv)
                      w = HtmlView()
                      w.load(QUrl("https://gmail.com"));
                      w.show()
                      sys.exit(app.exec_())
                  

                  我希望它会在任何网页上单击 target='_blank' 时打开一个新窗口.

                  I expect it to open a new window on click of target='_blank' on any webpage.

                  推荐答案

                  你必须重写 createWindow 方法并返回一个 QWebEngineView,但是为了不破坏对象,它必须是另一个窗口的子窗口或者是具有更长生命周期的容器.

                  You have to override the createWindow method and return a QWebEngineView, but for the object not to be distruded it must be the child of another window or be part of a container that has a longer life cycle.

                  from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
                  
                  class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
                      def acceptNavigationRequest(self, url,  _type, isMainFrame):
                          if _type == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked:
                              return True
                          return super(WebEnginePage, self).acceptNavigationRequest(url, _type, isMainFrame)
                  
                  class HtmlView(QtWebEngineWidgets.QWebEngineView):
                      def __init__(self, windows, *args, **kwargs):
                          super(HtmlView, self).__init__(*args, **kwargs)
                          self.setPage(WebEnginePage(self))
                          self._windows = windows
                          self._windows.append(self)
                  
                      def createWindow(self, _type):
                          if QtWebEngineWidgets.QWebEnginePage.WebBrowserTab:
                              v = HtmlView(self._windows)
                              v.resize(640, 480)
                              v.show()
                              return v
                  
                  if __name__ == '__main__':
                      import sys
                  
                      app = QtWidgets.QApplication(sys.argv)
                      windows = []
                      w = HtmlView(windows)
                      w.load(QtCore.QUrl("https://gmail.com"));
                      w.show()
                      sys.exit(app.exec_())
                  

                  这篇关于窗口未在外部 url 链接上打开新窗口或选项卡单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我在哪里为 Qt 设计器的单个提升 QWidget 编写类 下一篇:使用按键单击时如何更改pyqt5中的按钮文本

                  相关文章

                  最新文章

                    <small id='GSfgf'></small><noframes id='GSfgf'>

                  1. <legend id='GSfgf'><style id='GSfgf'><dir id='GSfgf'><q id='GSfgf'></q></dir></style></legend>
                      • <bdo id='GSfgf'></bdo><ul id='GSfgf'></ul>
                      <i id='GSfgf'><tr id='GSfgf'><dt id='GSfgf'><q id='GSfgf'><span id='GSfgf'><b id='GSfgf'><form id='GSfgf'><ins id='GSfgf'></ins><ul id='GSfgf'></ul><sub id='GSfgf'></sub></form><legend id='GSfgf'></legend><bdo id='GSfgf'><pre id='GSfgf'><center id='GSfgf'></center></pre></bdo></b><th id='GSfgf'></th></span></q></dt></tr></i><div id='GSfgf'><tfoot id='GSfgf'></tfoot><dl id='GSfgf'><fieldset id='GSfgf'></fieldset></dl></div>
                      <tfoot id='GSfgf'></tfoot>