Skip to content Skip to sidebar Skip to footer

Window Does Not Open New Window Or Tab On External Url Link Click

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 shoul

Solution 1:

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

Post a Comment for "Window Does Not Open New Window Or Tab On External Url Link Click"